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; } }
时间: 2024-10-19 12:15:08