Csharp/C#教程:在序列化XML时更改元素的顺序分享


在序列化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   

相关资源:

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

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

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

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

上述就是C#学习教程:在序列化XML时更改元素的顺序分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1009364.html

(0)
上一篇 2021年12月30日
下一篇 2021年12月30日

精彩推荐