Question

I need to Deserialize an XML that contains the definition of a Profile/polygon of points and arcs.

<Pt>
    <X>0.0</X>
    <Y>0.0</Y>
</Pt>
<Arc>
    <X>0.0</X>
    <Y>0.0</Y>
    <Rad>0.0</Rad>
</Arc>

Actual XML file:

<Profile>
    <Pt>...</Pt>
    <Pt>...</Pt>
    <Arc>...</Arc>
    <Pt>...</Pt>
    <Arc>...</Arc>
</Profile>

I am a little stuck finding a solution to know the order of child elements with different names. I am currently using something a schema that looks like the below code, but I obviously get individual arrays of Pts and Arcs without knowing the order of the Pts and Arcs. I have tried creating a substitutionGroup (Point) that takes both Pt and Arc, but I was not successful.

<xs:element name="Profile">
  <xs:complexType>
    <xs:sequence>
      <xs:element maxOccurs="unbounded" name="Pt">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="X" type="xs:decimal" />
            <xs:element name="Z" type="xs:decimal" />
          </xs:sequence>
        </xs:complexType>
      </xs:element>
      <xs:element maxOccurs="unbounded" name="Arc">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="X" type="xs:decimal" />
            <xs:element name="Z" type="xs:decimal" />
            <xs:element name="Rad" type="xs:decimal" />
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>
</xs:element>
Was it helpful?

Solution

I suppose you want to permit an arbitrary order of your Pt and Arc elements. The following schema will do the trick by setting maxOccurs="unbounded" in the sequence declaration. You will also have to correct your Pt and Arc declarations since currently they are using Z and not Y as in your data.

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:complexType name="pt-type">
    <xs:sequence>
      <xs:element name="X" type="xs:decimal" />
      <xs:element name="Y" type="xs:decimal" />
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="arc-type">
    <xs:sequence>
      <xs:element name="X" type="xs:decimal" />
      <xs:element name="Y" type="xs:decimal" />
      <xs:element name="Rad" type="xs:decimal" />
    </xs:sequence>
  </xs:complexType>

  <xs:element name="Profile">
    <xs:complexType>
      <xs:sequence maxOccurs="unbounded">
        <xs:choice >
          <xs:element name="Pt" type="pt-type"/>
          <xs:element name="Arc" type="arc-type"/>
        </xs:choice>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

</xs:schema>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top