Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
272 views
in Technique[技术] by (71.8m points)

c# - Change the order of elements when serializing XML

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

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

1 Reply

0 votes
by (71.8m points)

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:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...