3

我正在使用以下代码来验证使用 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())
        ;
}
4

1 回答 1

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.

但是,如果您&lt;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 验证器(至少是股票验证器)&lt;stillwrong>现在不会抱怨,因为它并不真正知道要匹配哪个 XSD 节点。

关键是可能有错误会导致验证者放弃做事,从而显示它好像跳过了一些人可能不希望做的事情。

如果对于我发布的场景,您的代码得到了列出的所有错误,那么您的代码就是它可以在带有内置验证器的 .NET 上的所有代码。如果您没有得到我列出的所有内容,那么我也错过了您的问题。

于 2013-02-27T18:49:46.383 回答