C# 针对 XSD 验证简单 XML 在使用前缀时不起作用



我正在尝试使用 C# 中的 XSD 验证 XML 文件:

C# 代码:

XmlReaderSettings booksSettings = new XmlReaderSettings();
XmlSchema xs = booksSettings.Schemas.Add("urn.mota:ndd.acu.gisae.VicuSolution111", "validation.xsd");
booksSettings.ValidationType = ValidationType.Schema;
booksSettings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings;
booksSettings.ValidationEventHandler += new ValidationEventHandler(valEventHandler);
XmlReader books = XmlReader.Create("data.xml", booksSettings);
while (books.Read()) {  }
Console.ReadLine();

数据.xml:

<VicuSolution111 xmlns:ns0="urn.mota:ndd.acu.gisae.VicuSolution111">
<ns0:Header>
<ns0:TransactionType></ns0:TransactionType>
</ns0:Header>
</VicuSolution111>

验证.xsd:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="urn.mota:ndd.acu.gisae.VicuSolution111"
elementFormDefault="qualified">
<xs:element name="VicuSolution111">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="1" name="Header">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="1" maxOccurs="1" name="TransactionType">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
<xs:maxLength value="40"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

当我使用报告验证警告执行验证时,我收到以下消息:

Warning Could not find schema information for the element 'VicuSolution111'.
Warning Could not find schema information for the element 'urn.mota:ndd.acu.gisae.VicuSolution111:Header'.
Warning Could not find schema information for the element 'urn.mota:ndd.acu.gisae.VicuSolution111:TransactionType'.

但是由于空的事务类型节点,我希望得到一个错误。如果我修改数据.xml通过添加不带 ns0 前缀的xmlns="urn.mota:ndd.acu.gisae.VicuSolution111",验证有效。

<VicuSolution111 xmlns:ns0="urn.mota:ndd.acu.gisae.VicuSolution111"
xmlns="urn.mota:ndd.acu.gisae.VicuSolution111">
<ns0:Header>
<ns0:TransactionType></ns0:TransactionType>
</ns0:Header>
</VicuSolution111>

这将给出预期的消息:

Error: The 'urn.mota:ndd.acu.gisae.VicuSolution111:TransactionType' element is invalid - The value '' is invalid according to its datatype 'String' - The actual length is less than the MinLength value.

有谁知道为什么会发生这种情况,以及如何在不修改原始 XML 文件并保留 xmlns:ns0 的情况下验证文档?

首先,您的模式没有目标命名空间,如果您更改它,那么它大部分都有效,即

<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid XML 2017 Liquid Studio - Data Designer Edition 15.0.0.6978 (https://www.liquid-technologies.com)-->
<xs:schema xmlns="urn.mota:ndd.acu.gisae.VicuSolution111" 
elementFormDefault="qualified" 
targetNamespace="urn.mota:ndd.acu.gisae.VicuSolution111" 
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="VicuSolution111">
<xs:complexType>
<xs:sequence>
<xs:element name="Header" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="TransactionType" minOccurs="1" maxOccurs="1">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1" />
<xs:maxLength value="40" />
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

但是你的XML是错误的,如果你像这样修改它,它会起作用

<?xml version="1.0" encoding="utf-8" ?>
<ns0:VicuSolution111  xmlns:ns0="urn.mota:ndd.acu.gisae.VicuSolution111">
<ns0:Header>
<ns0:TransactionType></ns0:TransactionType>
</ns0:Header>
</ns0:VicuSolution111>

如果你的原始XML是你想要的输出,那么你需要将你的模式分成2个文件,一个包含VicuSolution111,没有目标命名空间,另一个包含目标命名空间设置为'urn.mota:ndd.acu.gisae.VicuSolution111'的Header。

偶然地,解析器找不到 VicuSolution111 的架构定义(在您的 xml 文件中没有命名空间)的原因是,您告诉解析器架构表示命名空间 'urn.mota:ndd.acu.gisae.VicuSolution111' 中的项目。

XmlSchema xs = booksSettings.Schemas.Add("urn.mota:ndd.acu.gisae.VicuSolution111", "validation.xsd");

最新更新