在 .NET Core 中处理 XSD 验证

问题描述 投票:0回答:1

我有这个示例 XSD。它可以工作,但是当发生验证错误时,它会显示错误,如下所示。我也想要字段名称:

验证错误:“FIELD”元素无效 - 根据其数据类型“Union”,值“12345”无效 - 根据联合的任何成员类型,值“12345”无效。

XSD

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
    <xs:element name="DOCUMENTS">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="DOCUMENT">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="FORM" type="xs:string"/>
                            <xs:element name="HEADER">
                                <xs:complexType>
                                    <xs:sequence>
                                       <xs:element name="FIELD" maxOccurs="unbounded">
                                          <xs:complexType>
                                             <xs:simpleContent>
                                                <xs:extension base="FieldValueType">
                                                    <xs:attribute name="name" type="FieldNameType" use="required"/>
                                                </xs:extension>
                                             </xs:simpleContent>
                                          </xs:complexType>
                                       </xs:element>
                                    </xs:sequence>
                                </xs:complexType>
                            </xs:element>
                            <xs:element name="TABLES" minOccurs="0"/>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:simpleType name="FieldNameType">
        <xs:restriction base="xs:string">
            <xs:enumeration value="CALC_FILE"/>
            <xs:enumeration value="SQL_ORGNO"/>
            <xs:enumeration value="SQL_RECEIVER"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="FieldValueType">
        <xs:union memberTypes="CALC_FILEType SQL_ORGNOType SQL_RECEIVERType"/>
    </xs:simpleType>

    <xs:simpleType name="CALC_FILEType">
        <xs:restriction base="xs:string">
            <xs:pattern value="[A-Za-z ]+"/> 
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="SQL_RECEIVERType">
        <xs:restriction base="xs:string">
            <xs:pattern value="[A-Za-z ]+"/> 
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="SQL_ORGNOType">
        <xs:restriction base="xs:string">
            <xs:minLength value="5"/>
            <xs:maxLength value="8"/>
        </xs:restriction>
    </xs:simpleType>

</xs:schema>

XML

<?xml version="1.0" encoding="utf-8"?>
<DOCUMENTS>
  <DOCUMENT>
    <FORM>Development_template_DfD_1.8</FORM>
    <HEADER>
      <FIELD name="CALC_FILE">test</FIELD>
      <FIELD name="SQL_ORGNO">12345</FIELD>
      <FIELD name="SQL_RECEIVER">Test AS</FIELD>
    </HEADER>
    <TABLES />
  </DOCUMENT>
</DOCUMENTS>
c# xml xsd
1个回答
0
投票

如果您像

XmlDocument
一样验证一棵树,并深入研究以从验证异常中获取
XmlSchemaValidationException
,您可以将其
SourceObject
转换为
XmlNode
并读出其
OuterXml
(或者当然是某些属性)如果需要):

using System.Xml;
using System.Xml.Schema;

var xmlSchemaSet = new XmlSchemaSet();
xmlSchemaSet.Add(null, "schema1.xsd");


foreach (var file in new string[] { "valid-sample1.xml", "invalid-sample1.xml" })
{
    XmlDocument doc = new XmlDocument();
    doc.Load(file);

    doc.Schemas.Add(xmlSchemaSet);

    doc.Validate((o, e) =>
    {
        Console.WriteLine($"{file}: {e.Message} {((e.Exception as XmlSchemaValidationException).SourceObject as XmlNode).OuterXml}");
    });
}

对于无效样本,例如

<DOCUMENTS>
  <DOCUMENT>
    <FORM>Development_template_DfD_1.8</FORM>
    <HEADER>
      <FIELD name="CALC_FILE">test</FIELD>
      <FIELD name="SQL_ORGNO">1234</FIELD>
      <FIELD name="SQL_RECEIVER">Test AS</FIELD>
    </HEADER>
    <TABLES />
  </DOCUMENT>
</DOCUMENTS>

此输出例如

invalid-sample1.xml: The 'FIELD' element is invalid - The value '1234' is invalid according to its datatype 'Union' - The value '1234' is not valid according to any of the memberTypes of the union. <FIELD name="SQL_ORGNO">1234</FIELD>
© www.soinside.com 2019 - 2024. All rights reserved.