XML反序列化

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

XML:



 
   
     
       
        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反序列化
/// 
/// 
/// xml
/// 
public static T XmlDeserialize(string xml)
{
    try
    {
        using (StringReader sr = new StringReader(xml))
        {
            XmlSerializer xmldes = new XmlSerializer(typeof(T));
            return (T)xmldes.Deserialize(sr);
        }
    }
    catch
    {
        return default(T);
    }
}
/// 
/// XML反序列化
/// 
/// 
/// XML文件路径
/// 
public static T DeserializeFile(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);
    }
}

你可能感兴趣的:(XML反序列化)