码迷,mamicode.com
首页 > 其他好文 > 详细

xml转model

时间:2016-11-08 17:32:44      阅读:440      评论:0      收藏:0      [点我收藏+]

标签:mat   tom   oom   span   get   class   zab   eof   1.0   

            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

标签:mat   tom   oom   span   get   class   zab   eof   1.0   

原文地址:http://www.cnblogs.com/Harvard-L/p/6043393.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!