在序列化XML时更改元素的顺序

我需要将Object序列化为XML并返回。 XML已修复,我无法更改它。 我在bookingList之后无法生成此结构。

如何将这些元素“分组”显示为LIST,并在元素列表之前保留

看我的例子:

我需要的结构….

  1234567 Jil Sander 1    20  1234567890   2345678901     

结构我得到下面的C#代码….

  1234567 Jil Sander 1     1234567890   2345678901    20    

C#代码:

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

尝试使用XmlElementAttribute bookingList类的属性,以便控制该类的对象将如何序列化为XML

这是一个例子:

 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; } } 

在我的测试中,我获得了这个输出:

   sample 0  1   2   3   

相关资源:

  • 使用属性控制XML序列化

我正面临着这个问题并且我解决了它…它非常有趣,这可能是.net中的一个错误。

问题出在这里: public List booking= new List();

你应该使用: public List booking { get; set; } public List booking { get; set; }

你会得到定义的订单….但为什么呢? 谁知道… :)