2014-09-29 58 views
2

我已經創造了一些XSD模式和接收試圖從一個在C#閱讀時,下面的錯誤:XML元素具有無效子元素「X」,預計「X」

元素PartnerPSTNTransfer在命名空間'http://localhost/Orders-PartnerPSTNTransfer-v1-0'有命名空間'http://localhost/Orders-Connection-v1-0'中的無效子元素'StartDate'
期望的可能元素列表:名稱空間中的'StartDate''http://localhost/Orders-PartnerPSTNTransfer-v1-0'

但是,期望的元素是StartDate,它存在於xml文件中,所以我不確定爲什麼會顯示此錯誤。我會爲attatch我創建的模式的的.xsd:

<?xml version="1.0" encoding="utf-8"?> 
<xs:schema id="PartnerPSTN" 
    targetNamespace="http://localhost/Orders-PartnerPSTN-v1.0" 
    elementFormDefault="qualified" 
    xmlns="http://localhost/Orders-PartnerPSTN-v1.0" 
    xmlns:mstns="http://localhost/Orders-PartnerPSTN-v1.0" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:a="http://localhost/Orders-Address-v1-0" 
    xmlns:c="http://localhost/Orders-Common-v1-0" > 

    <xs:simpleType name="Contract"> 
    <xs:restriction base="xs:token"> 
     <xs:enumeration value="Monthly_12"></xs:enumeration> 
    </xs:restriction> 
    </xs:simpleType> 

    <xs:simpleType name="PackageOption"> 
    <xs:restriction base="xs:string"> 
     <xs:enumeration value="Data Only" /> 
     <xs:enumeration value="Free Evening and Weekend" /> 
     <xs:enumeration value="1000 Anytime" /> 
    </xs:restriction> 
    </xs:simpleType> 

    <xs:simpleType name="FeatureOption"> 
    <xs:list> 
     <xs:simpleType> 
     <xs:restriction base="xs:string"> 
      <xs:enumeration value="Feature0"/> 
      <xs:enumeration value="Feature0.A"/> 
      <xs:enumeration value="Feature0.B"/> 
      <xs:enumeration value="Feature0.C"/> 
      <xs:enumeration value="Feature1"/> 
      <xs:enumeration value="Feature2"/> 
      <xs:enumeration value="Feature2.A"/> 
      <xs:enumeration value="Feature3"/> 
      <xs:enumeration value="Feature3.A"/> 
      <xs:enumeration value="Feature3.B"/> 
      <xs:enumeration value="Feature3.C"/> 
     </xs:restriction> 
     </xs:simpleType> 
    </xs:list> 
    </xs:simpleType> 

    <xs:complexType name="PartnerPSTNConfiguration"> 
    <xs:sequence> 
     <xs:element name="Package" type="PackageOption" /> 
     <xs:element name="Feature" type="FeatureOption" /> 
    </xs:sequence> 
    </xs:complexType> 
</xs:schema> 

模式2:

<?xml version="1.0" encoding="utf-8"?> 
<xs:schema id="PartnerPSTNTransfer" 
    targetNamespace="http://localhost/Orders-PartnerPSTNTransfer-v1-0" 
    elementFormDefault="qualified" 
    xmlns="http://localhost/Orders-PartnerPSTNTransfer-v1-0" 
    xmlns:mstns="http://localhost/Orders-PartnerPSTNTransfer-v1-0" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:a="http://localhost/Orders-Address-v1-0" 
    xmlns:padsl="http://localhost/Orders-PartnerPSTN-v1.0" 
    xmlns:c="http://localhost/Orders-Common-v1-0" 
    xmlns:conn="http://localhost/Orders-Connection-v1-0"> 

    <xs:import namespace="http://localhost/Orders-Common-v1-0" schemaLocation="../../Common.xsd" /> 
    <xs:import namespace="http://localhost/Orders-PartnerPSTN-v1.0" schemaLocation="PartnerPSTN.xsd" /> 

    <xs:complexType name="PartnerPSTNTransfer"> 
    <xs:sequence> 
     <xs:element name="TelephoneNumber" type="c:Landline" /> 
     <xs:element name="StartDate" type="xs:date" /> 
     <xs:element name="Postcode" type="c:RequiredString" /> 
     <xs:element name="Configuration" type="padsl:PartnerPSTNConfiguration" /> 
    </xs:sequence> 
    </xs:complexType> 

    <xs:element name="PartnerPSTNTransfer" type="PartnerPSTNTransfer"></xs:element> 
</xs:schema> 

編輯

這裏是我的XML我想驗證:

<p:PartnerPSTNTransfer xmlns:padsl="http://localhost/Orders-PartnerPSTN-v1.0" 
xmlns:p="http://localhost/Orders-PartnerPSTNTransfer-v1-0" 
xmlns:a="http://localhost/Orders-Address-v1-0" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:c="http://localhost/Orders-Connection-v1-0"> 
    <p:TelephoneNumber>01111111111</p:TelephoneNumber> 
    <c:StartDate>2014-10-10T00:00:00</c:StartDate> 
    <c:Postcode>XX1 1XX</c:Postcode> 
    <p:Configuration> 
    <padsl:Contract>MA</padsl:Contract> 
    <padsl:PackageOption>Weekend</padsl:PackageOption> 
    <padsl:Featureoption>Feature0 Feature2.A</padsl:Featureoption> 
    </p:Configuration> 
</p:PartnerPSTNTransfer> 

編輯2

到目前爲止的XML。我beleive我已經刪除了原來的問題,雖然現在說:

The element 'Configuration' in namespace 'http://localhost/Orders-PartnerPSTNTransfer-v1-0' has invalid child element 'Contract' in namespace 'http://localhost/Orders-PartnerPSTN-v1.0'. 

和XML之中:

<p:PartnerPSTNTransfer xmlns:padsl="http://localhost/Orders-PartnerPSTN-v1.0" 
xmlns:p="http://localhost/Orders-PartnerPSTNTransfer-v1-0" 
xmlns:a="http://localhost/Orders-Address-v1-0" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:c="http://localhost/Orders-Connection-v1-0"> 
    <p:TelephoneNumber>01111111111</p:TelephoneNumber> 
    <p:StartDate>2014-10-10</p:StartDate> 
    <p:Postcode>EX20 1LP</p:Postcode> 
    <p:Configuration> 
    <padsl:Package>Free Evening and Weekend</padsl:Package> 
    <padsl:Feature>Feature0 Feature2.A</padsl:Feature> 
    <padsl:Contract>Monthly_12</padsl:Contract> 
    </p:Configuration> 
</p:PartnerPSTNTransfer> 
+1

什麼是您試圖驗證的實際XML實例? – 2014-09-29 10:03:06

+0

@TomRedfern我添加了我想驗證的XML – user667430 2014-09-29 10:06:16

+0

Schema 2在命名空間'http:// localhost/Orders-PartnerPSTNTransfer-v1-0'中定義了StartDate,XML在命名空間'http:// localhost'中提供了StartDate /訂單-連接-v1-0'。 – GSerg 2014-09-29 10:17:10

回答

4

XML實例表示,他們會尋找所謂的起始日期的元件,其在某處定義

<c:StartDate>2014-10-10T00:00:00</c:StartDate> 

其中:

命名空間 http://localhost/Orders-Connection-v1-0模式下

你的模式,但是,這種元素定義的命名空間http://localhost/Orders-PartnerPSTNTransfer-v1-0

所以你需要讓起始日期引用它包含類型正確的命名空間下定義的修改實例下屬於一個名爲PartnerPSTNTransfer類型。你已經提到,在「P」的命名空間前綴下根級別的命名空間,所以你應該能夠做到:

<p:StartDate>2014-10-10T00:00:00</p:StartDate> 

(與同爲郵編元素)。

編輯

在回答你問題的第二部分,XML實例解析爲命名空間http://localhost/Orders-PartnerPSTN-v1.0

但是在架構PartnerPSTNConfiguration定義並在類型PartnerPSTNConfiguration定義的元素稱爲合同不包含合同的定義,僅適用於包裝和特徵:

<xs:complexType name="PartnerPSTNConfiguration"> 
    <xs:sequence> 
    <xs:element name="Package" type="PackageOption" /> 
    <xs:element name="Feature" type="FeatureOption" /> 
    </xs:sequence> 
</xs:complexType> 

因此,要有效,您必須或者

<xs:complexType name="PartnerPSTNConfiguration"> 
    <xs:sequence> 
    ... 
    <xs:element name="Contract" type="SomeType" /> 
    </xs:sequence> 
</xs:complexType> 

,改變你的實例不能指望它在那裏:○合同元素在PartnerPSTNConfiguration類型定義模型,例如。

+0

啊,我明白了,對不起我對XML不太好。我剛剛更新了我的帖子,發現類似的錯誤是我在更改這兩個文件後得到的。和新的XML。問題出在:'' – user667430 2014-09-29 10:29:17

相關問題