Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I need to serialize an Object to XML and back. The XML is fix and I can't change it. I fail to generate this structure after bookingList.

How can I "group" these <booking> elements to appear as a LIST and keep <error> & <counter> before this List of <booking> elements.

See my example here:

Structure i need....

<nicexml>
<key_id>1234567</key_id>
<surname>Jil</surname>
<name>Sander</name>
<station_id>1</station_id>
<ownBookings>
    <bookingList>
        <error></error>
        <counter>20</counter>
        <booking>
             <bookingID>1234567890</bookingID>
        </booking>
        <booking>
             <bookingID>2345678901</bookingID>
        </booking>
    </bookingList>
</ownBookings>
</nicexml>

Structure i get with C# code below....

<nicexml>
<key_id>1234567</key_id>
<surname>Jil</surname>
<name>Sander</name>
<station_id>1</station_id>
<ownBookings>
    <bookingList>
           <booking>
        <booking>
             <bookingID>1234567890</bookingID>
        </booking>
        <booking>
             <bookingID>2345678901</bookingID>
        </booking>
             <booking>
        <error></error>
        <counter>20</counter>
    </bookingList>
</ownBookings>
</nicexml>

C# Code:

using System;
using System.Xml.Serialization;
using System.Collections.Generic;

namespace xml_objects_serials
{
    public class bookings
    {
        public class nicexml
        {
            public string key_id
            { get; set; }

            public string surname
            { get; set; }

            public string name
            { get; set; }

            public int station_id
            { get; set; }

            public ownBookings ownBookings
            { get; set; }

        }

        public class ownBookings
        {
            public bookingList bookingList
            { get; set; }

        }
        public class bookingList {
            public string error 
            { get; set; }
            public int counter
            { get; set; }
            public List<booking> booking= new List<booking>();
        }

        public class booking
        {
            public int bookingID
            { get; set; }
        }
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
263 views
Welcome To Ask or Share your Answers For Others

1 Answer

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:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...