首页 > 代码库 > model转xml
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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。