Change the order of elements when serializing XML

Try decorating the properties of the bookingListclass with the XmlElementAttribute, in order to control how the objects of that class are going to be serialized to XML.

Here’s an example:

public class bookingList
{
    [XmlElement(Order = 1)]
    public string error { get; set; }
    [XmlElement(Order = 2)]
    public int counter { get; set; }
    [XmlElement(ElementName = "booking", Order = 3)]
    public List<booking> bookings = new List<booking>();
}

public class booking
{
    public int id { get; set; }
}

In my test I obtained this output:

<?xml version="1.0" ?> 
<bookingList>
    <error>sample</error>
    <counter>0</counter>
    <booking>
        <id>1</id> 
    </booking>
    <booking>
        <id>2</id> 
    </booking>
    <booking>
        <id>3</id> 
    </booking> 
</bookingList>

Related resources:

  • Controlling XML Serialization Using Attributes

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)