反序列化带属性和值的xml节点集合

解析XML如下,需要取得Location节点的值和属性 


    
        
            
                http://pavo.elongstatic.com/i/API350_350/633e7d224c5eb5585c66f1fd9b33a02e.jpg
                http://pavo.elongstatic.com/i/Hotel70_70/0000j0nt.jpg
                http://pavo.elongstatic.com/i/Hotel120_120/0000j0nt.jpg
                http://pavo.elongstatic.com/i/Mobile640_960/0000j0nt.jpg
            
        
        
            true
            
                http://pavo.elongstatic.com/i/API350_350/73757abaff632f006bddc9317a16c0f6.jpg
                http://pavo.elongstatic.com/i/Hotel70_70/0000ipe7.jpg
                http://pavo.elongstatic.com/i/Hotel120_120/0000ipe7.jpg
                http://pavo.elongstatic.com/i/Mobile640_960/0000ipe7.jpg
            
        
    
        
        
    

实体类

using System;
using System.Collections.Generic;
using System.Xml.Serialization;

namespace ElongDataCache.Model
{
	[XmlRoot(ElementName = "Hotel")]
    public class ResHotelDetail
    {
        /// 
        ///详情
        /// 
        [XmlAttribute]
        public string Id { get; set; }

        /// 
        ///图片
        /// 
        [XmlArray("Images"), XmlArrayItem("Image")]
        public List Images { get; set; }
	}
	  [XmlRoot("Image")]
    public class Image
    {
        /// 
        ///关联的房型
        /// 
        [XmlAttribute]
        public string RoomId { get; set; }

        /// 
        ///图片类型
        /// 
        [XmlAttribute]
        public string Type { get; set; }

        /// 
        ///是否是主图
        /// 
        [XmlAttribute]
        public string IsCoverImage { get; set; }

        /// 
        ///图片地址
        /// 
        //[XmlArray("Locations"), XmlArrayItem("Location")]
        [XmlArray("Locations")]
        [XmlArrayItem("Location", typeof(Location))]
        public List Locations { get; set; }

        /// 
        ///作者类型
        /// 
        [XmlAttribute]
        public string AuthorType { get; set; }
    }

    [Serializable()]
    public class Location
    {
        /// 
        ///图片路径
        /// 
        [XmlText]
        public string Value { get; set; }

        /// 
        ///图片规格
        /// 
        [XmlAttribute]
        public string SizeType { get; set; } = "1";

        /// 
        /// 是否有水印
        /// 
        [XmlAttribute]
        public string WaterMark { get; set; }
    }
}

解析:

 /// 
        /// 反序列化
        /// 
        /// XML文件路径
        /// 
        public static object DeserializeFile(string xmlFile)
        {
            try
            {
                using (StreamReader sr = new StreamReader(xmlFile))
                {
                    XmlSerializer xmldes = new XmlSerializer(typeof(T));
                    return xmldes.Deserialize(sr);
                }
            }
            catch (Exception)
            {
                return null;
            }
        }
		
		 private void button3_Click(object sender, EventArgs e)
        {
            // 定义下载路径
            string savePath = Environment.CurrentDirectory + "\\";
            ResHotelDetail detail = (ResHotelDetail)DeserializeFile(savePath + "90594615.xml");
        }


你可能感兴趣的:(C#,xml,反序列化,节点属性,节点值)