我正在使用以下代码使用XSD架构文件验证XML文件。
它基本上有效。但是,当我尝试构建任何验证错误的列表时,我发现发生的任何验证错误都会引发异常,导致无法检测到进一步的验证错误。
我实际上是在LINQPad中运行它。谁能看到我错过的东西?
var settings = new XmlReaderSettings
{
ValidationType = ValidationType.Schema,
ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings
};
List<ValidationEventArgs> validationErrorsAndWarnings = new List<ValidationEventArgs>();
settings.ValidationEventHandler += (sender, eventArgs) => validationErrorsAndWarnings.Add(eventArgs);
settings.Schemas.Add(
targetNamespace: DataFeedXmlns,
schemaDocument: XmlReader.Create(new StringReader(DataFeedXsd)));
using (var xmlReader = XmlReader.Create(new StringReader(DataFeedXml), settings)) {
while (xmlReader.Read())
;
}
答案 0 :(得分:2)
我认为你的期望是关闭的(代码似乎很好)。这就是我的意思:考虑下面的XSD:
<?xml version="1.0" encoding="utf-8" ?>
<!-- XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com) -->
<xsd:schema targetNamespace="http://tempuri.org/XMLSchema.xsd" xmlns="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="root">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="tryme" minOccurs="0" maxOccurs="unbounded">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:pattern value="[a-z]+"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="really">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="wrong"/>
<xsd:element name="stillwrong">
<xsd:simpleType>
<xsd:restriction base="xsd:int"/>
</xsd:simpleType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
<xsd:attribute name="version" fixed="1"/>
</xsd:complexType>
</xsd:element>
</xsd:schema>
这个示例XML:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!-- Sample XML generated by QTAssistant (http://www.paschidev.com) -->
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1" xmlns="http://tempuri.org/XMLSchema.xsd">
<tryme>tryme1</tryme>
<tryme>tryme1</tryme>
<really>
<wrong/>
<stillwrong>a</stillwrong>
</really>
</root>
您的代码应报告三个错误:
Error occurred while loading [], line 4 position 17
The 'http://tempuri.org/XMLSchema.xsd:tryme' element is invalid - The value 'tryme1' is invalid according to its datatype 'String' - The Pattern constraint failed.
Error occurred while loading [], line 5 position 17
The 'http://tempuri.org/XMLSchema.xsd:tryme' element is invalid - The value 'tryme1' is invalid according to its datatype 'String' - The Pattern constraint failed.
Error occurred while loading [], line 8 position 18
The 'http://tempuri.org/XMLSchema.xsd:stillwrong' element is invalid - The value 'a' is invalid according to its datatype 'Int' - The string 'a' is not a valid Int32 value.
Document1.xml is XSD 1.0 invalid.
但是,如果从样本中删除<wrong/>
元素,即:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!-- Sample XML generated by QTAssistant (http://www.paschidev.com) -->
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1" xmlns="http://tempuri.org/XMLSchema.xsd">
<tryme>tryme1</tryme>
<tryme>tryme1</tryme>
<really>
<stillwrong>a</stillwrong>
</really>
</root>
您(大多数)可能会得到的错误现在是:
Error occurred while loading [], line 4 position 17
The 'http://tempuri.org/XMLSchema.xsd:tryme' element is invalid - The value 'tryme1' is invalid according to its datatype 'String' - The Pattern constraint failed.
Error occurred while loading [], line 5 position 17
The 'http://tempuri.org/XMLSchema.xsd:tryme' element is invalid - The value 'tryme1' is invalid according to its datatype 'String' - The Pattern constraint failed.
Error occurred while loading [], line 7 position 4
The element 'really' in namespace 'http://tempuri.org/XMLSchema.xsd' has invalid child element 'stillwrong' in namespace 'http://tempuri.org/XMLSchema.xsd'. List of possible elements expected: 'wrong' in namespace 'http://tempuri.org/XMLSchema.xsd'.
Document1.xml is XSD 1.0 invalid.
虽然数字相同,但.NET验证程序(至少是库存验证程序)现在不会抱怨<stillwrong>
,因为它并不真正知道要与哪个XSD节点匹配。
重点是可能存在错误,导致验证者放弃做某事,因此表明它似乎跳过了一些人可能不会想到的东西。
如果对于我发布的场景,您的代码会获得所列出的所有错误,那么您的代码就是使用内置验证器在.NET上的代码。如果你没有得到我列出的所有内容,那么我也错过了你的问题。