XML .xsd无效元素

时间:2014-10-03 12:19:17

标签: c# xml xsd

嗨我有一些xml,我正在尝试使用我创建的模式但是当我尝试解析xml时,我收到以下错误消息:

The element 'PartnerPSTNTransfer' in namespace 'http://localhost/Orders-PartnerPSTNTransfer-v1-0' has invalid child element 'InstallationAddress' in namespace 'http://localhost/Orders-PartnerPSTNTransfer-v1-0'. List of possible elements expected: 'Configuration' in namespace 'http://localhost/Orders-PartnerPSTNTransfer-v1-0'.

下面是我创建的XML以及与之相关的模式:

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>01224507392</p:TelephoneNumber>
  <p:StartDate>2014-10-25</p:StartDate>
  <p:Postcode>co27pe</p:Postcode>
  <p:InstallationAddress>
    <a:NameNumber>10</a:NameNumber>
    <a:Line1>Somewhere Road</a:Line1>
    <a:City>Somewhere City</a:City>
    <a:County>Somewhere County</a:County>
    <a:Postcode>co2 7pe</a:Postcode>
  </p:InstallationAddress>
  <p:Configuration>
    <padsl:Package>Data Only</padsl:Package>
    <padsl:Feature>F0 F1 F2</padsl:Feature>
    <padsl:Contract>Monthly_12</padsl:Contract>
  </p:Configuration>
</p:PartnerPSTNTransfer>

PartnerPSTNTransfer xsd:

<?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-Address-v1-0" schemaLocation="../../Address.xsd" />
  <xs:import namespace="http://localhost/Orders-PartnerPSTN-v1.0" schemaLocation="PartnerPSTN.xsd" />
  <xs:import namespace="http://localhost/Orders-Connection-v1-0" schemaLocation="../Connection.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="InstallationAddress" type="a:Address" />
      <xs:element name="Configuration" type="padsl:PartnerPSTNConfiguration" />
    </xs:sequence>
  </xs:complexType>

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

PSTNTransfer 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:import namespace="http://localhost/Orders-Common-v1-0" schemaLocation="../../Common.xsd"/>

  <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="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="F0"/>
          <xs:enumeration value="F0A"/>
          <xs:enumeration value="F0B"/>
          <xs:enumeration value="F0C"/>
          <xs:enumeration value="F1"/>
          <xs:enumeration value="F2"/>
          <xs:enumeration value="F2A"/>
          <xs:enumeration value="F3"/>
          <xs:enumeration value="F3A"/>
          <xs:enumeration value="F3B"/>
          <xs:enumeration value="F3C"/>
        </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:element name="Contract" type="Contract" />
    </xs:sequence>
  </xs:complexType>
</xs:schema>

我在Configuration元素之前的正确位置声明了InstallationAddress元素,因此对于它为什么会抛出此错误感到困惑。

1 个答案:

答案 0 :(得分:1)

使用xsi:schemaLocation向XML处理器提示http://localhost/Orders-PartnerPSTNTransfer-v1-0命名空间的XSD为PartnerPSTNTransfer.xsd

<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"
                       xsi:schemaLocation="http://localhost/Orders-PartnerPSTNTransfer-v1-0  
                                           PartnerPSTNTransfer.xsd">

在进行上述更改并删除未包含在您的问题中的XSD的悬空引用后,我能够验证您的XML文件。可能是您的XSD的旧版本从硬盘驱动器上的意外位置被拾取。请注意外部XML目录或其他工具机制,以便将XML文档实例与XSD相关联。