zoukankan      html  css  js  c++  java
  • 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; }
        }
  • 相关阅读:
    由类型名得到该类型
    TypeName of CA1505 is AvoidUnmaintainableCode, not AvoidUnmantainableCode.
    多核
    SSRS throws "The operation has timed out."
    XmlRootAttribute与XmlTypeAttribute
    littleendian and bigendian
    Assembly Exp
    如何在Ubuntu 11.10下成功安装Java [转载]
    *uck up ~,纯发泄
    [转载]读书相关,,
  • 原文地址:https://www.cnblogs.com/Harvard-L/p/6043393.html
Copyright © 2011-2022 走看看