xml字符串如下图:
怎么定义一个类似以下的实体类:
public class OrderStatListResponse : VipResponse
{
/// <summary>
/// 返回状态
/// </summary>
[XmlElement("status")]
public int Statu { get; set; }
///// <summary>
///// 搜索到的交易信息列表
///// </summary>
[XmlArray("data")]
[XmlArrayItem("list")]
public List<OrderList> List { get; set; }
}
public class OrderResponse { [XmlElement("status")] public Int32 Status { get; set; } [XmlElement("data")] public Data Data { get; set; } } public class Data { [XmlArray("list")] [XmlArrayItem("item")] public List<ListItem> Items { get; set; } } public class ListItem { [XmlElement("order_sn")] public String OrderSN { get; set; } [XmlElement("stat")] public String Status { get; set; } [XmlElement("address")] public String Address { get; set; } [XmlElement("tel")] public String Tel { get; set; } }
加个Data对象做为data结点
测试xml内容
<?xml version="1.0" encoding="utf-8" ?> <root> <status>13333</status> <data> <list> <item> <order_sn>99999</order_sn> <stat>123</stat> <address>北京</address> <tel>4009999999</tel> </item> <item> <order_sn>88888</order_sn> <stat>345</stat> <address>上海</address> <tel>34567</tel> </item> </list> </data> </root>
测试方法
public static OrderResponse DoTest2() { var rootAttribute = new XmlRootAttribute("root"); rootAttribute.Namespace = ""; XmlSerializer xs = new XmlSerializer(typeof(OrderResponse), rootAttribute); var path2 = AppDomain.CurrentDomain.BaseDirectory + "data.xml"; using (var fs = new FileStream(path2, FileMode.Open, FileAccess.Read)) { var orderResposne2 = (OrderResponse)xs.Deserialize(fs); return orderResposne2; } }
如果返回的json格式如下:
{"status":1,"data":{"list":[{"order_sn":"01022623872733","stat":"22","address":"\u897f\u85cf\u81ea\u6cbb\u533a.
\u62c9\u8428\u5e02.\u57ce\u5173\u533a\u5a18\u70ed\u8def17\u53f7\u897f\u85cf\u81ea\u6cbb\u533a
\u5ba1\u8ba1\u5385\u6295\u8d44\u5904","buyer":"\u8d75\u6d01","postcode":"850000","city":"\u62c9\u8428\u5e02","state":"\u89
7f\u85cf\u81ea\u6cbb
\u533a","country_id":"CN","tel":"","mobile":"13800138000","transport_day":"\u9001\u8d27\u65f6\u95f4\u4e0d
\u9650","remark":"","invoice":"\u4e2a
\u4eba","goods_money":"366.00","favourable_money":"0.00","ex_fav_money":"0.00","carriage":"0.00","vendor_id":"4916","vendo
r_name":"\u6df1\u5733\u5e02\u74a7\u661f\u7f51\u7edc\u79d1\u6280\u6709\u9650\u516c\u53f8","add_time":"2013-12-26
00:11:59","po":"2000162715"},{"order_sn":"01022556422613","stat":"22","address":"\u5e7f\u4e1c\u7701.\u6c5f\u95e8\u5e02.
\u84ec\u6c5f\u533a\u5824\u4e1c\u8def73\u53f7\u65af\u6d3e\u7279\u5927\u53a6 12\u697c","buyer":"\u674e\u5c0f
\u59d0","postcode":"529000","city":"\u6c5f\u95e8\u5e02","state":"\u5e7f\u4e1c
\u7701","country_id":"CN","tel":"","mobile":"13929045676","transport_day":"\u53ea\u5de5\u4f5c\u65e5(\u53cc\u4f11\u65e5\/
\u8282\u5047\u65e5\u4e0d
\u7528\u9001)","remark":"","invoice":"","goods_money":"739.00","favourable_money":"0.00","ex_fav_money":"0.00","carriage":
"0.00","vendor_id":"4916","vendor_name":"\u6df1\u5733\u5e02\u74a7\u661f\u7f51\u7edc\u79d1\u6280\u6709\u9650\u516c
\u53f8","add_time":"2013-12-25 23:26:04","po":"2000162715"},
{"order_sn":"13122573563841","stat":"22","address":"\u5c71\u897f\u7701\u592a\u539f\u5e02\u5c0f\u5e97\u533a\u5c0f
\u5e97\u8857\u9053\u957f\u6cbb\u8def\u5c45\u7136\u4e4b\u5bb6\u4e00\u5c42\u6728\u5730\u677f\u533a\u6c49\u8bfa
\u5730\u677f1092\u53f7","buyer":"\u5218\u9633","postcode":"030000","city":"\u592a\u539f\u5e02","state":"\u5c71\u897f
\u7701","country_id":"CN","tel":"","mobile":"13800138000","transport_day":"\u9001\u8d27\u65f6\u95f4\u4e0d
\u9650","remark":"","invoice":"","goods_money":"882.00","favourable_money":"0.00","ex_fav_money":"0.00","carriage":"0.00",
"vendor_id":"4916","vendor_name":"\u6df1\u5733\u5e02\u74a7\u661f\u7f51\u7edc\u79d1\u6280\u6709\u9650\u516c
\u53f8","add_time":"2013-12-25 23:10:37","po":"2000162715"}],"total":"100"}}
如何来写这个实体类,以便2种数据结构都能通用呢。
@晴.天:
json的话可以使用:JSON.NET
public class OrderResponse { [XmlElement("status")] [JsonProperty("status")] public Int32 Status { get; set; } [XmlElement("data")] [JsonProperty("data")] public Data Data { get; set; } } public class Data { [XmlArray("list")] [XmlArrayItem("item")] [JsonProperty("list")] public List<ListItem> Items { get; set; } } public class ListItem { [XmlElement("order_sn")] [JsonProperty("order_sn")] public String OrderSN { get; set; } [XmlElement("stat")] [JsonProperty("stat")] public String Status { get; set; } [XmlElement("address")] [JsonProperty("address")] public String Address { get; set; } [XmlElement("tel")] [JsonProperty("tel")] public String Tel { get; set; } } //测试代码: public static void SeralizeToJson() { var path = AppDomain.CurrentDomain.BaseDirectory + "orderResponse.json"; var orderResponse = new OrderResponse { Status = 111, Data = new Data { Items = new List<ListItem> { new ListItem{ OrderSN = "1111", Address = "北京", Status = "123", Tel = "2222" }, new ListItem{ OrderSN = "1111", Address = "北京", Status = "123", Tel = "2222" } } } }; var json = JsonConvert.SerializeObject(orderResponse); using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write)) { var bytes = System.Text.Encoding.UTF8.GetBytes(json); fs.Write(bytes, 0, bytes.Length); } } 结果: /* {"status":111,"data":{"list":[{"OrderSN":"1111","Status":"123","Address":"北京","Tel":"2222"},{"OrderSN":"1111","Status":"123","Address":"北京","Tel":"2222"}]}} */
@晴.天:
结果改下:
{"status":111,"data":{"list":[{"order_sn":"1111","stat":"123","address":"北京","tel":"2222"},{"order_sn":"1111","stat":"123","address":"北京","tel":"2222"}]}}