user667430
user667430

Reputation: 1547

XML element has invalid child element 'X', expected 'X'

I have created some XSD schemas and receive the below error when trying to read from it in C#:

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

However the expected element is StartDate and it is present in the xml file so I am unsure why this error would be shown. I'll attatch the .xsd for the schemas i created:

<?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>

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>

EDIT

Here is my XML I am trying to validate:

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

EDIT 2

XML so far. I beleive I've removed the original problem although it now says:

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

and the XML being:

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

Upvotes: 3

Views: 9969

Answers (1)

tom redfern
tom redfern

Reputation: 31760

Your xml instance says it's looking for an element called StartDate which is defined somewhere in a schema under the namespace http://localhost/Orders-Connection-v1-0:

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

where:

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

Your schema, however, defines this element as belonging to a type called PartnerPSTNTransfer under the namespace http://localhost/Orders-PartnerPSTNTransfer-v1-0

So you need to modify your instance so that StartDate references the correct namespace it's containing type was defined under. You have already referenced the namespace at root level under the namespace prefix of "p" so you should be able to do:

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

(And the same for PostCode element).

EDIT

In response to the second part of your question, the xml instance resolves to an element called Contract defined in type PartnerPSTNConfiguration in namespace http://localhost/Orders-PartnerPSTN-v1.0

However the definition for PartnerPSTNConfiguration in the schema does not contain a definition for Contract, only for Package and Feature:

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

So to be valid you have to model the Contract element in the PartnerPSTNConfiguration type definition, eg:

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

Or, change your instance to not expect it to be there.

Upvotes: 4

Related Questions