XML反序列化

短信平台和支付中心在与某些第三方接口通信时,涉及到xml格式的报文。这时会用到xml反序列化成相应实体对象。

XML:

<?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>  
  <Rooms> 
    <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>

实体类(注意,必须声明为public):

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 { getset; }  
  
        /// <summary>  
        ///图片  
        /// </summary>  
        [XmlArray("Images"), XmlArrayItem("Image")]  
        public List<Image> Images { getset; }  
    }  
    [XmlRoot("Image")]  
    public class Image  
    {  
        /// <summary>  
        ///关联的房型  
        /// </summary>  
        [XmlAttribute]  
        public string RoomId { getset; }  
  
        /// <summary>  
        ///图片类型  
        /// </summary>  
        [XmlAttribute]  
        public string Type { getset; }  
  
        /// <summary>  
        ///是否是主图  
        /// </summary>  
        [XmlAttribute]  
        public string IsCoverImage { getset; }  
  
        /// <summary>  
        ///图片地址  
        /// </summary>  
        //[XmlArray("Locations"), XmlArrayItem("Location")]  
        [XmlArray("Locations")]  
        [XmlArrayItem("Location"typeof(Location))]  
        public List<Location> Locations { getset; }  
  
        /// <summary>  
        ///作者类型  
        /// </summary>  
        [XmlAttribute]  
        public string AuthorType { getset; }  
    }  
  
    [Serializable()]  
    public class Location  
    {  
        /// <summary>  
        ///图片路径  
        /// </summary>  
        [XmlText]  
        public string Value { getset; }  
  
        /// <summary>  
        ///图片规格  
        /// </summary>  
        [XmlAttribute]  
        public string SizeType { getset; } = "1";  
  
        /// <summary>  
        /// 是否有水印  
        /// </summary>  
        [XmlAttribute]  
        public string WaterMark { getset; }  
    }  
}  

解析工具方法:

/// <summary>
/// XML反序列化
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="xml">xml</param>
/// <returns></returns>
public static T XmlDeserialize<T>(string xml)
{
    try
    {
        using (StringReader sr = new StringReader(xml))
        {
            XmlSerializer xmldes = new XmlSerializer(typeof(T));
            return (T)xmldes.Deserialize(sr);
        }
    }
    catch
    {
        return default(T);
    }
}
/// <summary>
/// XML反序列化
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="xmlFile">XML文件路径</param>
/// <returns></returns>
public static T DeserializeFile<T>(string xmlFile)
{
    try
    {
        using (StreamReader sr = new StreamReader(xmlFile))
        {
            XmlSerializer xmldes = new XmlSerializer(typeof(T));
            return (T)xmldes.Deserialize(sr);
        }
    }
    catch (Exception)
    {
        return default(T);
    }
}
原文地址:https://www.cnblogs.com/buguge/p/7155931.html