<small id='oW2QN'></small><noframes id='oW2QN'>

<legend id='oW2QN'><style id='oW2QN'><dir id='oW2QN'><q id='oW2QN'></q></dir></style></legend>

        <bdo id='oW2QN'></bdo><ul id='oW2QN'></ul>
    1. <tfoot id='oW2QN'></tfoot>
    2. <i id='oW2QN'><tr id='oW2QN'><dt id='oW2QN'><q id='oW2QN'><span id='oW2QN'><b id='oW2QN'><form id='oW2QN'><ins id='oW2QN'></ins><ul id='oW2QN'></ul><sub id='oW2QN'></sub></form><legend id='oW2QN'></legend><bdo id='oW2QN'><pre id='oW2QN'><center id='oW2QN'></center></pre></bdo></b><th id='oW2QN'></th></span></q></dt></tr></i><div id='oW2QN'><tfoot id='oW2QN'></tfoot><dl id='oW2QN'><fieldset id='oW2QN'></fieldset></dl></div>

        在 C# 中针对引用的 XSD 验证 XML

        时间:2023-06-04

              <tbody id='aWniW'></tbody>
            1. <small id='aWniW'></small><noframes id='aWniW'>

              <tfoot id='aWniW'></tfoot>

                  <bdo id='aWniW'></bdo><ul id='aWniW'></ul>
                  <i id='aWniW'><tr id='aWniW'><dt id='aWniW'><q id='aWniW'><span id='aWniW'><b id='aWniW'><form id='aWniW'><ins id='aWniW'></ins><ul id='aWniW'></ul><sub id='aWniW'></sub></form><legend id='aWniW'></legend><bdo id='aWniW'><pre id='aWniW'><center id='aWniW'></center></pre></bdo></b><th id='aWniW'></th></span></q></dt></tr></i><div id='aWniW'><tfoot id='aWniW'></tfoot><dl id='aWniW'><fieldset id='aWniW'></fieldset></dl></div>
                  <legend id='aWniW'><style id='aWniW'><dir id='aWniW'><q id='aWniW'></q></dir></style></legend>

                • 本文介绍了在 C# 中针对引用的 XSD 验证 XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我有一个带有指定架构位置的 XML 文件,例如:

                  I have an XML file with a specified schema location such as this:

                  xsi:schemaLocation="someurl ..localSchemaPath.xsd"
                  

                  我想在 C# 中进行验证.当我打开文件时,Visual Studio 会根据架构验证它并完美地列出错误.但是,不知何故,如果不指定要验证的架构,我似乎无法在 C# 中自动验证它:

                  I want to validate in C#. Visual Studio, when I open the file, validates it against the schema and lists errors perfectly. Somehow, though, I can't seem to validate it automatically in C# without specifying the schema to validate against like so:

                  XmlDocument asset = new XmlDocument();
                  
                  XmlTextReader schemaReader = new XmlTextReader("relativeSchemaPath");
                  XmlSchema schema = XmlSchema.Read(schemaReader, SchemaValidationHandler);
                  
                  asset.Schemas.Add(schema);
                  
                  asset.Load(filename);
                  asset.Validate(DocumentValidationHandler);
                  

                  我不应该能够使用 XML 文件中指定的架构自动进行验证吗?我错过了什么?

                  Shouldn't I be able to validate with the schema specified in the XML file automatically ? What am I missing ?

                  推荐答案

                  您需要创建一个 XmlReaderSettings 实例,并在创建时将其传递给您的 XmlReader.然后可以订阅设置中的ValidationEventHandler来接收验证错误.您的代码最终将如下所示:

                  You need to create an XmlReaderSettings instance and pass that to your XmlReader when you create it. Then you can subscribe to the ValidationEventHandler in the settings to receive validation errors. Your code will end up looking like this:

                  using System.Xml;
                  using System.Xml.Schema;
                  using System.IO;
                  
                  public class ValidXSD
                  {
                      public static void Main()
                      {
                  
                          // Set the validation settings.
                          XmlReaderSettings settings = new XmlReaderSettings();
                          settings.ValidationType = ValidationType.Schema;
                          settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessInlineSchema;
                          settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessSchemaLocation;
                          settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
                          settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
                  
                          // Create the XmlReader object.
                          XmlReader reader = XmlReader.Create("inlineSchema.xml", settings);
                  
                          // Parse the file. 
                          while (reader.Read()) ;
                  
                      }
                      // Display any warnings or errors.
                      private static void ValidationCallBack(object sender, ValidationEventArgs args)
                      {
                          if (args.Severity == XmlSeverityType.Warning)
                              Console.WriteLine("	Warning: Matching schema not found.  No validation occurred." + args.Message);
                          else
                              Console.WriteLine("	Validation error: " + args.Message);
                  
                      }
                  }
                  

                  这篇关于在 C# 中针对引用的 XSD 验证 XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何识别控件的可见性是否被用户更改? 下一篇:如何从 xsd 生成 .NET 4.0 类?

                  相关文章

                  最新文章

                  <small id='FS8Ge'></small><noframes id='FS8Ge'>

                  <legend id='FS8Ge'><style id='FS8Ge'><dir id='FS8Ge'><q id='FS8Ge'></q></dir></style></legend>
                • <i id='FS8Ge'><tr id='FS8Ge'><dt id='FS8Ge'><q id='FS8Ge'><span id='FS8Ge'><b id='FS8Ge'><form id='FS8Ge'><ins id='FS8Ge'></ins><ul id='FS8Ge'></ul><sub id='FS8Ge'></sub></form><legend id='FS8Ge'></legend><bdo id='FS8Ge'><pre id='FS8Ge'><center id='FS8Ge'></center></pre></bdo></b><th id='FS8Ge'></th></span></q></dt></tr></i><div id='FS8Ge'><tfoot id='FS8Ge'></tfoot><dl id='FS8Ge'><fieldset id='FS8Ge'></fieldset></dl></div>
                • <tfoot id='FS8Ge'></tfoot>
                    <bdo id='FS8Ge'></bdo><ul id='FS8Ge'></ul>