微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

.Net5.0 中的 Xml 内联验证未找到 xsd

如何解决.Net5.0 中的 Xml 内联验证未找到 xsd

我想根据 XSD 文件验证一些 XML 文件

我被迫将框架从 .Net4.7.2 更改为 .Net5.0。

这是编写和验证 XML 文件代码

namespace XmlValidation
{
    class Programm
    {
        static void Main()
        {
            string file = "test.xml";

            using (var writer = new StreamWriter(file))
            using (var xmlWriter = XmlWriter.Create(writer,new XmlWriterSettings { Indent = true,IndentChars = "    " }))
            {
                new XmlSerializer(typeof(MyClass)).Serialize(xmlWriter,new MyClass() { Property1 = "Content" });
            }

            List<string> messages = new();
            XmlReaderSettings xmlReaderSettings = new()
            {
                ValidationType = ValidationType.Schema,ValidationFlags = XmlSchemaValidationFlags.ProcessInlineschema
                | XmlSchemaValidationFlags.ProcessSchemaLocation
                | XmlSchemaValidationFlags.ReportValidationWarnings
            };
            xmlReaderSettings.ValidationEventHandler += new ValidationEventHandler((sender,args) =>
            {
                Console.WriteLine(args.Message);
            });

            using (XmlReader reader = XmlReader.Create(file,xmlReaderSettings))
            {
                while (reader.Read()) ;
            }

        }
    }

    public class MyClass
    {
        [XmlAttribute("noNamespaceSchemaLocation",Namespace = XmlSchema.InstanceNamespace)]
        public string xmlShemaFile = "schema0.xsd";

        public string Property1 { get; set; }
        public string Property2 { get; set; }
    }
}

我得到了 xml:

<?xml version="1.0" encoding="utf-8"?>
<MyClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:noNamespaceSchemaLocation="schema0.xsd">
    <Property1>Content</Property1>
</MyClass>

并且 xsd 与 test.xml 位于同一目录中:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="MyClass" type="MyClass" />
  <xs:complexType name="MyClass">
    <xs:all>
      <xs:element minOccurs="1" maxOccurs="1" name="Property1" type="xs:string" />
      <xs:element minOccurs="1" maxOccurs="1" name="Property2" type="xs:string" />
    </xs:all>
  </xs:complexType>
</xs:schema>

结果是:

"Could not find schema information for the element 'MyClass'."
"Could not find schema information for the element 'Property1'."

同样适用于 .NetFramework 4.7.2。

"Der Inhalt des Elements 'MyClass' ist unvollständig. Erwartet wurde die Liste der möglichen Elemente: 'Property2'."

为什么这不适用于 .Net5.0?

解决方法

正如评论中提到的 Mickel KayMartin Honnen,添加一个 XmlResolver 就成功了:

XmlReaderSettings xmlReaderSettings = new()
{
    ValidationType = ValidationType.Schema,ValidationFlags = XmlSchemaValidationFlags.ProcessInlineSchema
    | XmlSchemaValidationFlags.ProcessSchemaLocation
    | XmlSchemaValidationFlags.ReportValidationWarnings,XmlResolver = new XmlUrlResolver(),};

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。