1. 程式人生 > >反序列化帶屬性和值的xml節點集合

反序列化帶屬性和值的xml節點集合

解析XML如下,需要取得Location節點的值和屬性 
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Hotel Id="123456456">
    <Images>
        <Image AuthorType="Hotel" Type="8" IsCoverImage="true" RoomId="0002">
            <Locations>
                <Location WaterMark="0" Size="1">http://pavo.elongstatic.com/i/API350_350/633e7d224c5eb5585c66f1fd9b33a02e.jpg</Location>
                <Location WaterMark="0" Size="2">http://pavo.elongstatic.com/i/Hotel70_70/0000j0nt.jpg</Location>
                <Location WaterMark="0" Size="3">http://pavo.elongstatic.com/i/Hotel120_120/0000j0nt.jpg</Location>
                <Location WaterMark="1" Size="7">http://pavo.elongstatic.com/i/Mobile640_960/0000j0nt.jpg</Location>
            </Locations>
        </Image>
        <Image AuthorType="Hotel" Type="8" RoomId="0002">
            <IsRoomCoverImage>true</IsRoomCoverImage>
            <Locations>
                <Location WaterMark="0" Size="1">http://pavo.elongstatic.com/i/API350_350/73757abaff632f006bddc9317a16c0f6.jpg</Location>
                <Location WaterMark="0" Size="2">http://pavo.elongstatic.com/i/Hotel70_70/0000ipe7.jpg</Location>
                <Location WaterMark="0" Size="3">http://pavo.elongstatic.com/i/Hotel120_120/0000ipe7.jpg</Location>
                <Location WaterMark="1" Size="7">http://pavo.elongstatic.com/i/Mobile640_960/0000ipe7.jpg</Location>
            </Locations>
        </Image>
    </Images>
        <Room Amount="1" Area="16" Description="大床1.5米、9樓、16平米、免費寬頻、可入住1人" BedType="大床1.5米" BroadnetFee="0" Capacity="1" Facilities="42,63,65,67,75,80,82,109,110,130,143,149,153,156,677,86,89" Floor="9" BroadnetAccess="1" Comments="長租房,不含水電費,物業費" Name="大床房" Id="0001"/>
        <Room Amount="1" Area="20" Description="雙床、9樓、20平米、免費寬頻、可入住2人" BedType="雙床" BroadnetFee="0" Capacity="2" Facilities="49,63,65,67,75,80,82,109,110,130,143,149,153,156,677,86,90" Floor="9" BroadnetAccess="1" Comments="; 1.5米; " Name="標準間" Id="0002"/>
    </Rooms>
</Hotel>

實體類

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

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

        /// <summary>
        ///圖片
        /// </summary>
        [XmlArray("Images"), XmlArrayItem("Image")]
        public List<Image> Images { get; set; }
	}
	  [XmlRoot("Image")]
    public class Image
    {
        /// <summary>
        ///關聯的房型
        /// </summary>
        [XmlAttribute]
        public string RoomId { get; set; }

        /// <summary>
        ///圖片型別
        /// </summary>
        [XmlAttribute]
        public string Type { get; set; }

        /// <summary>
        ///是否是主圖
        /// </summary>
        [XmlAttribute]
        public string IsCoverImage { get; set; }

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

        /// <summary>
        ///作者型別
        /// </summary>
        [XmlAttribute]
        public string AuthorType { get; set; }
    }

    [Serializable()]
    public class Location
    {
        /// <summary>
        ///圖片路徑
        /// </summary>
        [XmlText]
        public string Value { get; set; }

        /// <summary>
        ///圖片規格
        /// </summary>
        [XmlAttribute]
        public string SizeType { get; set; } = "1";

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

解析:
 /// <summary>
        /// 反序列化
        /// </summary>
        /// <param name="xmlFile">XML檔案路徑</param>
        /// <returns></returns>
        public static object DeserializeFile<T>(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<ResHotelDetail>(savePath + "90594615.xml");
        }