model转xml

Posted 哈佛

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了model转xml相关的知识,希望对你有一定的参考价值。

   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的主要内容,如果未能解决你的问题,请参考以下文章

xml转model

条件片段和导航重用

通过按钮更改 viewpager 片段 Click

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

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

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