首页 > 代码库 > xml转model
xml转model
string xml = @"<?xml version=‘1.0‘ encoding=‘utf-8‘?> <priceRequest> <hotelId>16166</hotelId> <checkin>2014-12-28</checkin> <checkout>2014-12-30</checkout> <roomId>199</roomId> <numberOfRooms>2</numberOfRooms> <customerInfos> <customerInfo seq=‘0‘ numberOfAdults=‘2‘ numberOfChildren=‘2‘ childrenAges=‘8|12‘ > </customerInfo> <customerInfo seq=‘1‘ numberOfAdults=‘2‘ numberOfChildren=‘0‘ childrenAges=‘‘ > </customerInfo> </customerInfos> </priceRequest>"; PriceRequest list = XmlDeserialize<PriceRequest>(xml);
//反序列化 public static T XmlDeserialize<T>(string str) where T : class { object obj; using (System.IO.MemoryStream mem = new MemoryStream(Encoding.Default.GetBytes(str))) { using (XmlReader reader = XmlReader.Create(mem)) { XmlSerializer formatter = new XmlSerializer(typeof(T)); obj = formatter.Deserialize(reader); } } return obj as T; }
//model [Serializable] [XmlRoot("priceRequest")] public class PriceRequest { public string hotelId { get; set; } public string checkin { get; set; } public string checkout { get; set; } public string roomId { get; set; } public string numberOfRooms { get; set; } public customerInfos customerInfos; } public class customerInfos { [XmlElement("customerInfo")] public List<customerInfo> customerInfoList; } public class customerInfo { [XmlAttribute] public string seq { get; set; } [XmlAttribute] public string numberOfAdults { get; set; } [XmlAttribute] public string numberOfChildren { get; set; } [XmlAttribute] public string childrenAges { get; set; } }
xml转model
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。