【问题标题】:Validate XML with a XSD Schema without changing the XML using C#在不使用 C# 更改 XML 的情况下使用 XSD 架构验证 XML
【发布时间】:2011-02-16 08:14:36
【问题描述】:

我有一个在 XML 中没有架构的 XML 文件,我需要根据 XSD 架构验证 XML。我见过很多将 XSD 注入 XML 然后验证 XML 的示例。我不想更改 XML,是否可以在不更改 XML 的情况下针对架构验证 XML?

【问题讨论】:

    标签: c# validation xsd


    【解决方案1】:

    在 C# 中只需几行代码就可以轻松编写代码。

    我创建了一个简单的命令行界面实用程序,它接受两个参数:XML、XSD 并进行验证。

    你可以下载它here。

    主要代码如下:

                // 1- Read XML file content
                reader = new XmlTextReader(XMLPath);
    
                // 2- Read Schema file content
                StreamReader SR = new StreamReader(XSDPath);
    
                // 3- Create a new instance of XmlSchema object
                XmlSchema Schema = new XmlSchema();
                // 4- Set Schema object by calling XmlSchema.Read() method
                Schema = XmlSchema.Read(SR,
                    new ValidationEventHandler(ReaderSettings_ValidationEventHandler));
    
                // 5- Create a new instance of XmlReaderSettings object
                XmlReaderSettings ReaderSettings = new XmlReaderSettings();
                // 6- Set ValidationType for XmlReaderSettings object
                ReaderSettings.ValidationType = ValidationType.Schema;
                // 7- Add Schema to XmlReaderSettings Schemas collection
                ReaderSettings.Schemas.Add(Schema);
    
                // 8- Add your ValidationEventHandler address to
                // XmlReaderSettings ValidationEventHandler
                ReaderSettings.ValidationEventHandler +=
                    new ValidationEventHandler(ReaderSettings_ValidationEventHandler);
    
                // 9- Create a new instance of XmlReader object
                XmlReader objXmlReader = XmlReader.Create(reader, ReaderSettings);
    
    
                // 10- Read XML content in a loop
                while (objXmlReader.Read())
                { /*Empty loop*/}
    

    【讨论】:

    • 完美,通过一些更改、一些搜索和匿名类型,我创建了一个方法来完成我需要的一切。详情现在在我的博客上:bryanavery.co.uk/post/2010/05/06/… 感谢您的帮助
    【解决方案2】:

    您可以将架构添加到 xml 文档中

    doc.Schemas.Add(schema);
    

    然后验证它

    bool xmlvalid = true;
    string lastXmlError = "";
    
    doc.Validate(new System.Xml.Schema.ValidationEventHandler(
        delegate(object sender, System.Xml.Schema.ValidationEventArgs args)
        {
            if (args.Severity == System.Xml.Schema.XmlSeverityType.Error)
                {
                    xmlvalid = false;
                    lastXmlError = args.Message;
                }
        }));
    
    if (!xmlvalid)
       //raise error
    

    【讨论】:

      【解决方案3】:

      仅当您希望在第一个验证错误之后继续验证文档时才提供 ValidationEventHandler。否则,只需这样做:

      private bool ValidateDocument(string xmlFile, string xsdFile)
      {
          XmlReaderSettings settings = new XmlReaderSettings{ValidationType 
            = ValidationType.Schema};
          settings.Schemas.Add(XmlSchema.Read(XmlReader.Create(xsdFile)));
          XmlReader reader = XmlReader.Create(xmlFile, settings);
      
          try
          {
              while(reader.Read());
              return true;
          }
          catch (XmlException ex) 
          {
              // XmlException indicates a validation error occurred.
              return false;
          }
      }
      

      以下链接提供更多信息:

      http://msdn.microsoft.com/en-us/library/1xe0740a.aspx

      http://support.microsoft.com/kb/307379

      【讨论】:

        猜你喜欢
        • 2012-09-16
        • 1970-01-01
        • 1970-01-01
        • 2012-06-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多