首页 文章

如何使用xmlschemaset和xmlreader.create来针对xsd架构验证xml

提问于
浏览
3

我在我的程序中修复警告,显然xmlvalidating reader和xmlschemacollection已经过时了 . 问题是,我不太确定如何 . 这是尝试使用涉及xmlschemaset和xmlreader.create的新模板“模仿”先前的验证函数 . 我首先声明一个模式,然后使用targeturi字符串设置它,然后在设置验证事件处理程序时将其添加到模式集 . 我认为我的问题是设置读者和输入流 . 我知道如何使用xmlvalidating reader,但如果我想修复这些警告,那么这不是一个选项 . 这是代码和尝试 . 在测试期间,只使用了新的验证xml代码,旧的代码被注释掉了 .

// New Validation Xml.
            string xsd_file = filename.Substring(0, filename.Length - 3) + "xsd";
            XmlSchema xsd = new XmlSchema();
            xsd.SourceUri = xsd_file;

            XmlSchemaSet ss = new XmlSchemaSet();
            ss.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
            ss.Add(xsd);
            if (ss.Count > 0)
            {
                XmlTextReader r = new XmlTextReader(filename2);
                XmlReaderSettings settings = new XmlReaderSettings();
                settings.ValidationType = ValidationType.Schema;
                settings.Schemas.Add(ss);
                settings.ValidationEventHandler +=new ValidationEventHandler(ValidationCallBack);
                XmlReader reader = XmlReader.Create(filename2, settings);
                while (reader.Read())
                {
                }
                reader.Close();
            }

            // Old Validate XML
            XmlSchemaCollection sc = new XmlSchemaCollection();
            sc.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
            sc.Add(null, xsd_file);
            if (sc.Count > 0)
            {
                XmlTextReader r = new XmlTextReader(filename2);
                XmlValidatingReader v = new XmlValidatingReader(r);
                v.ValidationType = ValidationType.Schema;
                v.Schemas.Add(sc);
                v.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
                while (v.Read())
                {
                }
                v.Close();
            }

    private void ValidationCallBack(object sender, ValidationEventArgs e)
    {
        // If Document Validation Fails
        isvalid = false;
        MessageConsole.Text = "INVALID. Check message and datagridview table.";
        richTextBox1.Text = "The document is invalid: " + e.Message;
    }

不幸的是,当我运行程序并尝试验证无效的xml文档时,它会给出一个如下错误:“未声明'URNLookup'元素 . ” URNLookup元素是xml文件的根元素 . 我总是可以回到旧的验证方法,但那些警告吓到了我 .

任何帮助都非常感谢 . 先感谢您!如果我遗漏任何信息,我会很乐意提供更多信息 .

  • tf.rz(.NET 3.5 SP1,Visual Studio C#2008)

1 回答

  • 7

    我已经解决了这个问题,它现在又重新开始工作而没有任何警告 . 在新验证XML中:

    // New Validation Xml.
                string xsd_file = filename.Substring(0, filename.Length - 3) + "xsd";
                XmlSchema xsd = new XmlSchema();
                xsd.SourceUri = xsd_file;
    
                XmlSchemaSet ss = new XmlSchemaSet();
                ss.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
                ss.Add(null, xsd_file);
                if (ss.Count > 0)
                {
                    XmlReaderSettings settings = new XmlReaderSettings();
                    settings.ValidationType = ValidationType.Schema;
                    settings.Schemas.Add(ss);
                    settings.Schemas.Compile();
                    settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
                    XmlTextReader r = new XmlTextReader(filename2);
                    using (XmlReader reader = XmlReader.Create(r, settings))
                    {
                        while (reader.Read())
                        {
                        }
                    }
                }
    

    ss.add已更改为具有命名空间和文件字符串 . 添加了settings.schemas.compile(),并添加了“using(xmlreader reader ... ...”)的无关紧要的重组 .

    这页帮助了我很多:http://msdn.microsoft.com/en-us/library/fe6y1sfe(v=vs.80).aspx它现在有效 .

相关问题