c# - 异常阻止检测多个 XML 验证错误

标签 c# xml xsd

我使用以下代码通过 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())
        ;
}

最佳答案

我认为你的期望是错误的(代码看起来不错)。这就是我的意思:考虑下面的 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 上运行。如果您没有得到我列出的所有内容,那么我也错过了您的问题。

关于c# - 异常阻止检测多个 XML 验证错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15118989/

相关文章:

sql-server - SQL Server 将 XML 子节点 append 到父节点

xml - 如何格式化 XSD 文档?

xml - 如何使用 XPath 在 XML 文档中选择多组属性?

c# - 使用 LINQ C# 检查是否存在具有特定属性的任何 XML 节点

c# - 如何从我的 ASP.NET Core 应用程序中永久注销用户?

c# - 如果选择了 CheckAll 则全选,如果没有则使用 jQuery 从 Gridview 中取消全选

c# - 使用 Schematron 验证 XML

xml - XSD 中的无序元素具有强制性和无界元素?

c# - 使用可为空的枚举属性和具有空字符串值的附加选项呈现单选按钮

c# - 从 F# 初始化 C# 结构?