2012-02-19 73 views
0

我從XFA靜態表單中提取一些xml。 下面是一個示例:XML驗證c# - 複雜類型

<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根據Xsd讀取和驗證Xml。只要我沒有複雜的類型,一切都可以。但在這種情況下,我有。

可以做些什麼?

而我得到的錯誤是:

元素「GroundType」不能包含子元素的「價值」,因爲父元素的內容模型是空的。

+0

你的問題不明確;什麼是不好,你看到什麼特別的錯誤或意外的行爲? – 2012-02-19 18:12:29

+0

我剛剛添加了錯誤,感謝您指出了這一點 – 2012-02-19 18:15:50

回答

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> 
+0

感謝它的所有工作,這是一種遺憾,Xsd.exe默認不會這麼做! – 2012-02-19 19:46:25