Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to alternate XML Elements during XML serialization in C#?

I have an XSD schema that has been given to us by a data provider. I cannot modify it. I generated the classes using the XSD.exe command line tool. For everything it works perfectly, I can create my objects in C#, serialize it in XML and validate it against the XSD.

I have a problem with a small portion of it. The expected output is:

    <Physical>
        <Class>P</Class>
        <Capacity>14</Capacity>
        <Class>J</Class>
        <Capacity>64</Capacity>
        <Class>W</Class>
        <Capacity>1</Capacity>
        <Class>Y</Class>
        <Capacity>2</Capacity>
    </Physical>
    <Saleable Protected="true">
        <Class>P</Class>
        <Capacity>14</Capacity>
        <Class>J</Class>
        <Capacity>64</Capacity>
        <Class>W</Class>
        <Capacity>1</Capacity>
        <Class>Y</Class>
        <Capacity>2</Capacity>
    </Saleable>

As you can see, the child elements of Physical and Sealable alternate (i.e. Class, then Capacity, then Class, then Capacity, etc.).

This is the code of the class that was generated by XSD.exe:

public partial class ClassA
{
    private string[] classField;

    private Integerctype[] capacityField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("Class", DataType = "token")]
    public string[] Class
    {
        get
        {
            return this.classField;
        }
        set
        {
            this.classField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("Capacity", IsNullable = true)]
    public Integerctype[] Capacity
    {
        get
        {
            return this.capacityField;
        }
        set
        {
            this.capacityField = value;
        }
    }
}

And the output I receive after the serialization:

<Physical>
    <Class>P</Class>
    <Class>J</Class>
    <Class>W</Class>
    <Class>Y</Class>
    <Capacity>14</Capacity>
    <Capacity>64</Capacity>
    <Capacity>1</Capacity>
    <Capacity>2</Capacity>
</Physical>
<Saleable>
    <Class>P</Class>
    <Class>J</Class>
    <Class>W</Class>
    <Class>Y</Class>
    <Capacity>14</Capacity>
    <Capacity>64</Capacity>
    <Capacity>1</Capacity>
    <Capacity>2</Capacity>
</Saleable>

As you can see, we lost the alternation between Class and Capacity...

I tried to use the Order property of the XmlElementAttribute: the Class property was decorated with Order = 1, and the Capacity property was decorated with Order = 2, but it did not help. Example:

[System.Xml.Serialization.XmlElementAttribute("Class", DataType = "token", Order = 1)]
public string[] Class

During the validation, with or without the Order property, I receive errors as follow:

The element 'Physical' in namespace 'xxx' has invalid child element 'Class' in namespace 'xxx'. List of possible elements expected: 'Capacity' in namespace 'xxx'.

Finally, here is the portion of the XSD:

<xsd:element name="ClassA" minOccurs="0">
    <xsd:complexType>
        <xsd:all>
            <xsd:element name="Physical" minOccurs="0">
                <xsd:annotation>
                    <xsd:documentation>True, physical class A configuration</xsd:documentation>
                </xsd:annotation>
                <xsd:complexType>
                    <xsd:sequence minOccurs="0" maxOccurs="unbounded">
                        <xsd:element name="Class" type="CabinClass.type" />
                        <xsd:element name="Capacity" type="Integer.ctype" nillable="true" />
                    </xsd:sequence>
                    <xsd:attributeGroup ref="Array.attgroup" />
                </xsd:complexType>
            </xsd:element>
            <xsd:element name="Saleable" minOccurs="0">
                <xsd:annotation>
                    <xsd:documentation>Class A configuration for sales purposes</xsd:documentation>
                </xsd:annotation>
                <xsd:complexType>
                    <xsd:sequence minOccurs="0" maxOccurs="unbounded">
                        <xsd:element name="Class" type="CabinClass.type" />
                        <xsd:element name="Capacity" type="Integer.ctype" nillable="true" />
                    </xsd:sequence>
                    <xsd:attributeGroup ref="Array.attgroup" />
                </xsd:complexType>
            </xsd:element>
        </xsd:all>
        <xsd:attributeGroup ref="Container.attgroup" />
    </xsd:complexType>
</xsd:element>

My guess is that it is related to the presence of xsd:sequence. But as I said, I do not want to modify the XSD as it is provided by a data provider and we must ensure that the XML we generate is fully compatible.

Any idea how can I solve this problem?

like image 975
DotNetMatt Avatar asked Dec 07 '25 08:12

DotNetMatt


1 Answers

Simplified code could be this:

public class Physical
{
    [XmlElement("Capacity", typeof(int))]
    [XmlElement("Class", typeof(string))]
    public object[] Items { get; set; }
}

This will ensure correct deserialization and give the serialization of the elements in the order in which they are placed in the array.

A working version might look like this:

public class Physical
{
    [EditorBrowsable(EditorBrowsableState.Never)]
    [XmlElement("Capacity", typeof(int))]
    [XmlElement("Class", typeof(string))]
    public object[] Items
    {
        get
        {
            object[] items = new object[Class.Length * 2];
            for (int i = 0; i < items.Length; i += 2)
            {
                items[i] = Class[i / 2];
                items[i + 1] = Capacity[i / 2];
            }
            return items;
        }
        set
        {
            Class = new string[value.Length / 2];
            Capacity = new int[value.Length / 2];
            for (int i = 0; i < value.Length; i += 2)
            {
                Class[i / 2] = (string)value[i];
                Capacity[i / 2] = (int)value[i + 1];
            }
        }
    }

    [XmlIgnore]
    public string[] Class { get; set; }
    [XmlIgnore]
    public int[] Capacity { get; set; }
}

Change int to Integerctype, add DataType parameter.

Similarly, change the second class.

like image 63
Alexander Petrov Avatar answered Dec 09 '25 20:12

Alexander Petrov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!