繁体   English   中英

XML元素具有无效的子元素“ X”,预期为“ X”

[英]XML element has invalid child element 'X', expected 'X'

我已经创建了一些XSD架构,并尝试在C#中从中读取以下错误:

元素PartnerPSTNTransfer在命名空间'http://localhost/Orders-PartnerPSTNTransfer-v1-0'具有无效子元素'StartDate'在命名空间'http://localhost/Orders-Connection-v1-0'
预期可能元素的列表: 'StartDate'在命名空间'http://localhost/Orders-PartnerPSTNTransfer-v1-0'

但是,预期的元素是StartDate ,它存在于xml文件中,因此我不确定为什么会显示此错误。 我将为创建的模式添加.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。 我相信我已经删除了原来的问题,尽管现在它说:

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>

您的xml实例说它正在寻找一个名为StartDate的元素,该元素在名称空间http://localhost/Orders-Connection-v1-0下的架构中定义:

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

哪里:

xmlns:c="http://localhost/Orders-Connection-v1-0"

但是,您的架构将此元素定义为属于名称空间http://localhost/Orders-PartnerPSTNTransfer-v1-0下的一个名为http://localhost/Orders-PartnerPSTNTransfer-v1-0

因此,您需要修改实例,以便StartDate引用定义其所在类型的正确名称空间。 您已经在根目录级别在名称空间前缀“ p”下引用了该名称空间,因此您应该能够:

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

(与PostCode元素相同)。

编辑

响应您问题的第二部分,xml实例解析为在命名空间http://localhost/Orders-PartnerPSTN-v1.0 PartnerPSTNConfiguration类型中定义的名为Contract的元素。

但是,模式中PartnerPSTNConfiguration的定义不包含合同的定义,仅包含包和功能的定义:

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

因此,为了有效,您必须在PartnerPSTNConfiguration类型定义中对Contract元素建模,例如:

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

或者,更改您的实例以使其不存在。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM