Change the order of elements when serializing XML

前端 未结 2 439
我寻月下人不归
我寻月下人不归 2020-12-06 08:58

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 \"

2条回答
  •  情话喂你
    2020-12-06 09:43

    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 bookings = new List();
    }
    
    public class booking
    {
        public int id { get; set; }
    }
    

    In my test I obtained this output:

     
    
        sample
        0
        
            1 
        
        
            2 
        
        
            3 
         
    
    

    Related resources:

    • Controlling XML Serialization Using Attributes

提交回复
热议问题