Question

I am currently having a problem with an XSD. Normally an entry looks like this:

<Entry Num="4">
    <Info>
        <Name>Something</Name>
        <ID>1234</ID>
        <Start>2013-01-07</Start>
        <Stop>2013-01-09</Stop>
        <Completed>6</Completed>
    </Info>
</Entry>

But occasionally it will look like this:

<Entry Num="5">
    <Info>
        <Name>SomethingElse</Name>
        <ID>5678</ID>
        <Start>2013-01-08</Start>
        <Stop>2013-01-10</Stop>
        <Start>2013-01-11</Start>
        <Stop>2013-01-12</Stop>
        <Completed>14</Completed>
    </Info>
</Entry>

To try and capture the potential for multiple Starts and Stops I tried the following things:

<xs:sequence maxOccurs="unbounded">
    <xs:element name="Start" type="xs:dateTime" maxOccurs="1"/>
    <xs:element name="Stop" type="xs:dateTime" maxOccurs="1"/>
</xs:sequence>

<xs:sequence maxOccurs="unbounded">
    <xs:element name="Start" type="xs:dateTime" />
    <xs:element name="Stop" type="xs:dateTime" />
</xs:sequence>

<xs:sequence maxOccurs="unbounded">
    <xs:sequence>
        <xs:element name="Start" type="xs:dateTime" />
        <xs:element name="Stop" type="xs:dateTime" />
    </xs:sequence>
</xs:sequence>

<xs:sequence maxOccurs="unbounded">
    <xs:sequence>
        <xs:element name="Start" type="xs:dateTime" maxOccurs="1"/>
        <xs:element name="Stop" type="xs:dateTime" maxOccurs="1"/>
    </xs:sequence>
</xs:sequence>

But they all resulted in an array of Starts which are printed followed by an array of Stops when I converted it to C# classes using xsd.exe which serialized to this:

<Entry Num="5">
    <Info>
        <Name>SomethingElse</Name>
        <ID>5678</ID>
        <Start>2013-01-08</Start>
        <Start>2013-01-11</Start>
        <Stop>2013-01-10</Stop>
        <Stop>2013-01-12</Stop>
        <Completed>14</Completed>
    </Info>
</Entry>

And this doesn't match the XML file. Does anyone know how to do something like this properly? Thanks a lot.

I came up with a solution that works, but isn't ideal.

Current Solution:

<xs:choice minOccurs="2" maxOccurs="unbounded">
    <xs:element name="Start" type="xs:dateTime"/>
    <xs:element name="Stop" type="xs:dateTime"/>
</xs:choice>
Was it helpful?

Solution

You simply missed the /order argument.

Try something like this: xsd /c /order your.xsd

The output would be distinguishable from what you have by virtue of additional Order values:

[System.Xml.Serialization.XmlElementAttribute("Start", typeof(System.DateTime), DataType="date", Order=2)]
[System.Xml.Serialization.XmlElementAttribute("Stop", typeof(System.DateTime), DataType="date", Order=2)]
[System.Xml.Serialization.XmlChoiceIdentifierAttribute("ItemsElementName")]
public System.DateTime[] Items {
    get {
        return this.itemsField;
    }
    set {
        this.itemsField = value;
    }
}

A simple test program such as this would correctly roundtrip your XML:

using System;
using System.IO;
using System.Xml.Serialization;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlSerializer ser = new XmlSerializer(typeof(Entry));
            Entry o;
            using (Stream s = File.OpenRead(@"D:\...\representing-a-repeated-pair-of-xml-elements-in-xsd-2.xml"))
            {
                o = (Entry)ser.Deserialize(s);
            }
            using (Stream s = File.OpenWrite(@"D:\...\representing-a-repeated-pair-of-xml-elements-in-xsd-3.xml"))
            {
                ser.Serialize(s, o);
            }
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top