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

model转xml

时间:2016-11-08 17:14:02      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:ati   ret   type   text   private   encoding   add   serialize   turn   

   private static string DataToXml_String(string strReturn)
        {
            Hotel r = new Hotel();
            r.HotelAttributes = new List<HotelAttribute>();
            for (int i = 0; i < 3; i++)
            {
                HotelAttribute ha = new HotelAttribute();
                ha.id = i.ToString();
                ha.name = "name" + i.ToString();
                ha.tel = "tel" + i.ToString();
                ha.city = "city" + i.ToString();
                ha.address = "address" + i.ToString();
                ha.strValue = " ";

                r.HotelAttributes.Add(ha);
            }

            strReturn = XmlSerializerHelp.Serializer(typeof(Hotel), r).Replace("> <", "><");
            return strReturn;
        }
//格式
<?xml version="1.0" encoding="utf-8"?>
<list>
     <hotel id="0" tel="tel0" address="address0" name="name0" city="city0"></hotel>
     <hotel id="1" tel="tel1" address="address1" name="name1" city="city1"></hotel>
     <hotel id="2" tel="tel2" address="address2" name="name2" city="city2"></hotel>
</list>
//model
 [Serializable]
    [XmlRoot("list")]
    public class Hotel
    {
        [XmlElement("hotel")]
        public List<HotelAttribute> HotelAttributes;
    }

   [Serializable]
    public class HotelAttribute
    {
        // 节点属性 
        [XmlAttribute]
        public string id { get; set; }
        [XmlAttribute]
        public string tel { get; set; }
        [XmlAttribute]
        public string address { get; set; }
        [XmlAttribute]
        public string name { get; set; }
        [XmlAttribute]
        public string city { get; set; }
        //不常用 节点值
        [XmlText]
        public string strValue { get; set; }
    }

 

model转xml

标签:ati   ret   type   text   private   encoding   add   serialize   turn   

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

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