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

    • <bdo id='Wflqx'></bdo><ul id='Wflqx'></ul>

    <tfoot id='Wflqx'></tfoot>

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

        xmlserializer 验证

        时间:2023-06-04
      2. <legend id='SxcTp'><style id='SxcTp'><dir id='SxcTp'><q id='SxcTp'></q></dir></style></legend>

                <tbody id='SxcTp'></tbody>

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

            • <tfoot id='SxcTp'></tfoot>

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

                • <bdo id='SxcTp'></bdo><ul id='SxcTp'></ul>
                  本文介绍了xmlserializer 验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我正在使用 XmlSerializer 反序列化 Xml 档案.但我发现生成的类 xsd.exe 只提供读取 xml 的能力,但没有验证.例如,如果文档中缺少一个节点,则生成的类的属性字段将为空,而不是像我预期的那样抛出验证异常.我怎样才能做到这一点?谢谢!

                  I'm using XmlSerializer to deserialize Xml achives. But I found the class xsd.exe generated only offers capability to read the xml, but no validation. For example, if one node is missing in a document, the attribute field of the generated class will be null, rather than throws a validation exception as I expected. How can I achieve that? Thanks!

                  推荐答案

                  以下代码应在反序列化时针对架构进行验证.类似的代码可用于在序列化时针对模式进行验证.

                  The following code should validate against a schema while deserializing. Similar code can be used to validate against a schema while serializing.

                  private static Response DeserializeAndValidate(string tempFileName)
                  {
                      XmlSchemaSet schemas = new XmlSchemaSet();
                      schemas.Add(LoadSchema());
                  
                      Exception firstException = null;
                  
                      var settings = new XmlReaderSettings
                                     {
                                         Schemas = schemas,
                                         ValidationType = ValidationType.Schema,
                                         ValidationFlags =
                                             XmlSchemaValidationFlags.ProcessIdentityConstraints |
                                             XmlSchemaValidationFlags.ReportValidationWarnings
                                     };
                      settings.ValidationEventHandler +=
                          delegate(object sender, ValidationEventArgs args)
                          {
                              if (args.Severity == XmlSeverityType.Warning)
                              {
                                  Console.WriteLine(args.Message);
                              }
                              else
                              {
                                  if (firstException == null)
                                  {
                                      firstException = args.Exception;
                                  }
                  
                                  Console.WriteLine(args.Exception.ToString());
                              }
                          };
                  
                      Response result;
                      using (var input = new StreamReader(tempFileName))
                      {
                          using (XmlReader reader = XmlReader.Create(input, settings))
                          {
                              XmlSerializer ser = new XmlSerializer(typeof (Response));
                              result = (Response) ser.Deserialize(reader);
                          }
                      }
                  
                      if (firstException != null)
                      {
                          throw firstException;
                      }
                  
                      return result;
                  }
                  

                  这篇关于xmlserializer 验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:XSDObjectGen.exe 与 XSD.exe 下一篇:解析复杂的 WSDL 参数信息

                  相关文章

                  最新文章

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

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

                      • <bdo id='TKa2Q'></bdo><ul id='TKa2Q'></ul>
                      <tfoot id='TKa2Q'></tfoot>