Han*_*hal 23 c# xml list xml-serialization
我需要将Object序列化为XML并返回.XML已修复,我无法更改它.之后我无法生成这个结构bookingList.
我怎样才能"团"这些<booking>元素出现的列表中,并<error>与<counter>名单的前<booking>元素.
看我的例子:
我需要的结构....
<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>
Run Code Online (Sandbox Code Playgroud)
结构我得到下面的C#代码....
<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>
Run Code Online (Sandbox Code Playgroud)
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> booking= new List<booking>();
}
public class booking
{
public int bookingID
{ get; set; }
}
}
Run Code Online (Sandbox Code Playgroud)
Enr*_*lio 34
尝试bookingList使用XmlElementAttribute修饰类的属性,以便控制该类的对象将如何序列化为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<booking> bookings = new List<booking>();
}
public class booking
{
public int id { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
在我的测试中,我获得了这个输出:
<?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>
Run Code Online (Sandbox Code Playgroud)
相关资源:
| 归档时间: |
|
| 查看次数: |
17785 次 |
| 最近记录: |