如何使用XmlSchemaSet中并xmlreader.create验证XML对XSD架构 [英] How to use xmlschemaset and xmlreader.create to validate xml against an xsd schema

查看:141
本文介绍了如何使用XmlSchemaSet中并xmlreader.create验证XML对XSD架构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我固定在我的程序的警告,显然xmlvalidating读卡器和XmlSchemaCollection中已过时。问题是,我不太清楚如何。下面是在模仿以前的验证用新涉及的XmlSchemaSet和xmlreader.create功能的尝试。我首先声明一个模式,并使用targeturi弦设置,然后将其添加到schemaset在设置验证事件处理程序。我想我的问题是建立读者和输入流。我知道如何与xmlvalidating读者做到这一点,但是这是不是一种选择,如果我要修复这些警告。下面的代码和尝试。在测试过程中,只使用了新的验证XML代码,旧的被注释掉。

I'm fixing warnings in my program and apparently xmlvalidating reader and xmlschemacollection is obsolete. The problem is, I'm not quite sure how. Here's an attempt at "mimicking" the previous validation function with the new one involving xmlschemaset and xmlreader.create. I first declare a schema, and set it using the targeturi string, then add it to the schemaset while setting up the validation event handler. I think my problem is setting up the readers and the input streams. I knew how to do it with xmlvalidating reader, but that isn't an option if I want to fix these warnings. Here's the code and attempt. During testing, only the new validation xml code was used, the old one was commented out.

            // 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文件的根元素。我可以随时回到旧的验证方法,但这些警告吓唬我。

Unfortunately, when I run the program and try to validate an invalid xml document, it gives me an error like this: "The 'URNLookup' element is not declared." The URNLookup element is the root element of the xml file. I can always go back to the old validation method, but those warnings scare me.

任何帮助是极大的赞赏。先谢谢你!我会很乐意提供更多的信息,如果我错过任何

Any help is appreciated greatly. Thank you in advance! I'll be happy to provide additional information if I missed any.


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

推荐答案

我有固定的问题,它现在再次与没有警告的工作。
在新的验证XML:

I have fixed the issue and it is now working again with no warnings. In the New Validation 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(),并将该的微不足道重组使用(读者的XMLReader ...。增加了。

The ss.add was changed to have a namespace, and the file string. settings.schemas.compile() was added, and the insignificant reorganization of the "using(xmlreader reader.. . . ." was added.

这页对我的帮助很多: http://msdn.microsoft.com/en-us/library /fe6y1sfe(v=vs.80).aspx 现在它的工作原理。

This page helped me a lot: http://msdn.microsoft.com/en-us/library/fe6y1sfe(v=vs.80).aspx It now works.

这篇关于如何使用XmlSchemaSet中并xmlreader.create验证XML对XSD架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆