如何处理无效的子元素?

时间:2014-01-07 08:54:35

标签: c# xml io

我有几个这样的XML:

-<PURCHASE xmlns:odf="urn:xmlns:eAccepta:output-data-fields">


-<PO TYPE="PURCHASEORDER">

<CUSTPONUM>PO0000</CUSTPONUM>

<CUSTNUM/>

<CUSTNAME>Custom name</CUSTNAME>

<DATE>2014-01-06</DATE>

<CUSTPREFIX>5LAN</CUSTPREFIX>

<MEMO/>

</PO>


-<POLINES>


-<LINE>

<LINENUM>0</LINENUM>

<CUSTLINE>1</CUSTLINE>

<CUSTITEM>013169</CUSTITEM>

<ITEMREV>A</ITEMREV>

<DESCRIPTION>Description</DESCRIPTION>

<QTY>2</QTY>

<PRICE>46.14</PRICE>

<DUEDATE>2014-01-30</DUEDATE>

<OURITEM>gfd4fd</OURITEM>

<CUSTREF>013169</CUSTREF>

<ITEMMEMO/>

</LINE>

</POLINES>

</PURCHASE>

从这个XML我生成了一个XSD:

<?xml version="1.0" encoding="utf-16"?>
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="PURCHASE">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="PO">
          <xsd:complexType>
            <xsd:sequence>
              <xsd:element name="CUSTPONUM" type="xsd:string" />
              <xsd:element name="CUSTNUM" type="xsd:string" />
              <xsd:element name="CUSTNAME" type="xsd:string" />
              <xsd:element name="DATE" type="xsd:dateTime" />
              <xsd:element name="CUSTPREFIX" type="xsd:string" />
              <xsd:element name="MEMO" type="xsd:string" />
            </xsd:sequence>
            <xsd:attribute name="TYPE" type="xsd:string" />
          </xsd:complexType>
        </xsd:element>
        <xsd:element name="POLINES">
          <xsd:complexType>
            <xsd:sequence>
              <xsd:element name="LINE">
                <xsd:complexType>
                  <xsd:sequence>
                    <xsd:element name="LINENUM" type="xsd:int" />
                    <xsd:element name="CUSTLINE" type="xsd:int" />
                    <xsd:element name="CUSTITEM" type="xsd:int" />
                    <xsd:element name="ITEMREV" type="xsd:string" />
                    <xsd:element name="DESCRIPTION" type="xsd:string" />
                    <xsd:element name="QTY" type="xsd:int" />
                    <xsd:element name="PRICE" type="xsd:decimal" />
                    <xsd:element name="DUEDATE" type="xsd:dateTime" />
                    <xsd:element name="OURITEM" type="xsd:string" />
                    <xsd:element name="CUSTREF" type="xsd:int" />
                    <xsd:element name="ITEMMEMO" type="xsd:string" />
                  </xsd:sequence>
                </xsd:complexType>
              </xsd:element>
            </xsd:sequence>
          </xsd:complexType>
        </xsd:element>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

现在我的代码一直在抱怨这是一个无效的子元素。我从外部应用程序中获取XML,因此我无法更改XML的任何内容。

我处理XML的代码如下所示:

public void ValidateXmlDocument(string documentToValidate, string schemaPath)
        {
            XmlSchema schema;
            using (var schemaReader = XmlReader.Create(@"C:\XML\XMLSchema.xsd"))
            {
                schema = XmlSchema.Read(schemaReader, ValidationEventHandler);
            }

            var schemas = new XmlSchemaSet();
            schemas.Add(schema);

            var settings = new XmlReaderSettings();
            settings.ValidationType = ValidationType.Schema;
            settings.Schemas = schemas;
            settings.ValidationFlags =
                XmlSchemaValidationFlags.ProcessIdentityConstraints |
                XmlSchemaValidationFlags.ReportValidationWarnings;
            settings.ValidationEventHandler += ValidationEventHandler;

            using (var validationReader = XmlReader.Create(documentToValidate, settings))
            {
                while (validationReader.Read())
                {
                    Console.WriteLine("ValidateXmlDocument ok");
                }
            }
        }

        private static void ValidationEventHandler(
            object sender, ValidationEventArgs args)
        {
            if (args.Severity == XmlSeverityType.Error)
            {
                throw args.Exception;
            }

            Console.WriteLine(args.Message);
        }

我得到的错误是

The element 'POLINES' has invalid child element 'LINE'.

当我使用免费的在线XML验证器进行验证时,它告诉我XML对XSD文件是完全完美的。 虽然我的代码一直给我错误。

2 个答案:

答案 0 :(得分:0)

至少有一个问题是您错误地订购了Entry子元素。序列标记中定义的元素必须以相同的顺序出现在相应的XML文档中。

根据您的架构验证您的示例XML后,这是我看到的唯一问题。

编辑:

如果您无法控制输入文件的元素顺序&amp;它不一致&amp; Entry的每个子元素每个条目最多只能出现一次,您可能希望使用ALL元素而不是序列。

Source

答案 1 :(得分:0)

我通过将<xsd:element name="LINE">更改为<xsd:element maxOccurs="unbounded" name="LINE">

来自行修复

希望有一天能帮到某人。