我有幾個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文件來說是完全完美的。 雖然我的代碼不斷給我錯誤。
這不是一個選項,因爲可能會發生多次。如果出現0或1次,可以使用「全部」。也;我的孩子元素都按照我的XSD正確對應。 –
Matheno