xml转model

Posted 哈佛

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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的主要内容,如果未能解决你的问题,请参考以下文章

xml转model

条件片段和导航重用

通过按钮更改 viewpager 片段 Click

在Tomcat的安装目录下conf目录下的server.xml文件中增加一个xml代码片段,该代码片段中每个属性的含义与用途

很实用的JQuery代码片段(转)

几个有用的JavaScript/jQuery代码片段(转)