XML验证c# - 复杂类型

时间:2012-02-19 17:41:35

标签: xml xsd xml-parsing

我正在从XFA静态表单中提取一些xml。 这是一个示例:

<xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
<xfa:data>
<frmMain>
<InspectionDate>19/02/2012</InspectionDate>
<ENID>111114567</ENID>
<EmployeeNumber>1234</EmployeeNumber>
<GroundType>
   <value>Tarmac</value>
   <value>Concrete</value>
</GroundType>
<Width>800</Width>
<Height>900</Height>
<OtherDetails>Corssing of x road and y street</OtherDetails>
</frmMain>
</xfa:data>
</xfa:datasets>

我使用Windows SDK 7.0实用程序Xsd.exe生成架构,以便我可以验证该XML 这是:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="datasets" targetNamespace="http://www.xfa.org/schema/xfa-data/1.0/" xmlns:mstns="http://www.xfa.org/schema/xfa-data/1.0/" xmlns="http://www.xfa.org/schema/xfa-data/1.0/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" attributeFormDefault="qualified" elementFormDefault="qualified">
  <xs:attribute name="dataNode" msdata:Prefix="xfa" type="xs:string" />
  <xs:element name="datasets" msdata:IsDataSet="true" msdata:Locale="en-US" msdata:Prefix="xfa">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element name="data" msdata:Prefix="xfa">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="frmMain" form="unqualified">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="InspectionDate" form="unqualified" type="xs:string" minOccurs="0" />
                    <xs:element name="ENID" form="unqualified" type="xs:string" minOccurs="0" />
                    <xs:element name="EmployeeNumber" form="unqualified" type="xs:string" minOccurs="0" />
                    <xs:element name="Width" form="unqualified" type="xs:string" minOccurs="0" />
                    <xs:element name="Height" form="unqualified" type="xs:string" minOccurs="0" />
                    <xs:element name="OtherDetails" form="unqualified" type="xs:string" minOccurs="0" />
                    <xs:element name="GroundType" form="unqualified" minOccurs="0" maxOccurs="unbounded">
                      <xs:complexType>
                        <xs:attribute ref="mstns:dataNode" />
                      </xs:complexType>
                    </xs:element>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>

我使用XmlReaderSettings和XmlReader来读取和验证Xml对Xsd。只要我没有复杂的类型,一切都很好。但在这种情况下,我有。

可以做些什么?

我得到的错误是:

元素'GroundType'不能包含子元素'value',因为父元素的内容模型是空的。

1 个答案:

答案 0 :(得分:1)

您的GroundType定义应如下所示:

<xs:element name="GroundType" form="unqualified" minOccurs="0" maxOccurs="unbounded">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="value" form="unqualified" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
        </xs:sequence>
        <xs:attribute ref="mstns:dataNode"/>
    </xs:complexType>
</xs:element>