• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#XML反序列化与序列化举例:XmlSerializer

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
  1. using System;  
  2. using System.IO;  
  3. using System.Xml.Serialization;  
  4.   
  5. namespace XStream  
  6. {  
  7.     /// <summary>  
  8.     /// <remarks>Xml序列化与反序列化</remarks>  
  9.     /// <creator>zhangdapeng</creator>  
  10.     /// </summary>  
  11.     public class XmlSerializeUtil  
  12.     {  
  13.         #region 反序列化  
  14.         /// <summary>  
  15.         /// 反序列化  
  16.         /// </summary>  
  17.         /// <param name="type">类型</param>  
  18.         /// <param name="xml">XML字符串</param>  
  19.         /// <returns></returns>  
  20.         public static object Deserialize(Type type, string xml)  
  21.         {  
  22.             using (StringReader sr = new StringReader(xml))  
  23.             {  
  24.                 XmlSerializer xmldes = new XmlSerializer(type);  
  25.                 return xmldes.Deserialize(sr);  
  26.             }  
  27.         }  
  28.         /// <summary>  
  29.         /// 反序列化  
  30.         /// </summary>  
  31.         /// <param name="type"></param>  
  32.         /// <param name="xml"></param>  
  33.         /// <returns></returns>  
  34.         public static object Deserialize(Type type, Stream stream)  
  35.         {  
  36.             XmlSerializer xmldes = new XmlSerializer(type);  
  37.             return xmldes.Deserialize(stream);  
  38.         }  
  39.         #endregion  
  40.  
  41.         #region 序列化  
  42.         /// <summary>  
  43.         /// 序列化  
  44.         /// </summary>  
  45.         /// <param name="type">类型</param>  
  46.         /// <param name="obj">对象</param>  
  47.         /// <returns></returns>  
  48.         public static string Serializer(Type type, object obj)  
  49.         {  
  50.             MemoryStream Stream = new MemoryStream();  
  51.             XmlSerializer xml = new XmlSerializer(type);  
  52.             //序列化对象  
  53.             xml.Serialize(Stream, obj);  
  54.             Stream.Position = 0;  
  55.             StreamReader sr = new StreamReader(Stream);  
  56.             string str = sr.ReadToEnd();  
  57.   
  58.             sr.Dispose();  
  59.             Stream.Dispose();  
  60.   
  61.             return str;  
  62.         }  
  63.  
  64.         #endregion  
  65.     }  
  66. }  
using System;
using System.IO;
using System.Xml.Serialization;

namespace XStream
{
    /// <summary>
    /// <remarks>Xml序列化与反序列化</remarks>
    /// <creator>zhangdapeng</creator>
    /// </summary>
    public class XmlSerializeUtil
    {
        #region 反序列化
        /// <summary>
        /// 反序列化
        /// </summary>
        /// <param name="type">类型</param>
        /// <param name="xml">XML字符串</param>
        /// <returns></returns>
        public static object Deserialize(Type type, string xml)
        {
            using (StringReader sr = new StringReader(xml))
            {
                XmlSerializer xmldes = new XmlSerializer(type);
                return xmldes.Deserialize(sr);
            }
        }
        /// <summary>
        /// 反序列化
        /// </summary>
        /// <param name="type"></param>
        /// <param name="xml"></param>
        /// <returns></returns>
        public static object Deserialize(Type type, Stream stream)
        {
            XmlSerializer xmldes = new XmlSerializer(type);
            return xmldes.Deserialize(stream);
        }
        #endregion

        #region 序列化
        /// <summary>
        /// 序列化
        /// </summary>
        /// <param name="type">类型</param>
        /// <param name="obj">对象</param>
        /// <returns></returns>
        public static string Serializer(Type type, object obj)
        {
            MemoryStream Stream = new MemoryStream();
            XmlSerializer xml = new XmlSerializer(type);
            //序列化对象
            xml.Serialize(Stream, obj);
            Stream.Position = 0;
            StreamReader sr = new StreamReader(Stream);
            string str = sr.ReadToEnd();

            sr.Dispose();
            Stream.Dispose();

            return str;
        }

        #endregion
    }
}

这个类是个实体类

 

  1. using System.Collections.Generic;  
  2. using System.Xml.Serialization;  
  3.   
  4. namespace XStream  
  5. {  
  6.     /// <summary>  
  7.     /// <creator>zhangdapeng</creator>  
  8.     /// </summary>  
  9.     [XmlRoot("skycenter")]  
  10.     public class AdsbEntity  
  11.     {  
  12.         private string _type;  
  13.   
  14.         [XmlAttribute("type")]  
  15.         public string Type  
  16.         {  
  17.             get { return _type; }  
  18.             set { _type = value; }  
  19.         }  
  20.         private string _source;  
  21.         [XmlAttribute("source")]  
  22.         public string Source  
  23.         {  
  24.             get { return _source; }  
  25.             set { _source = value; }  
  26.         }  
  27.         private string _reality;  
  28.         [XmlAttribute("reality")]  
  29.         public string Reality  
  30.         {  
  31.             get { return _reality; }  
  32.             set { _reality = value; }  
  33.         }  
  34.         private string _rcvTime;  
  35.         [XmlAttribute("rcvTime")]  
  36.         public string RcvTime  
  37.         {  
  38.             get { return _rcvTime; }  
  39.             set { _rcvTime = value; }  
  40.         }  
  41.         private Head _head;  
  42.         //属性的定义  
  43.         [XmlElement("head")]  
  44.         public Head Head  
  45.         {  
  46.             set   //设定属性  
  47.             {  
  48.                 _head = value;  
  49.             }  
  50.             get    //从属性获取值  
  51.             {  
  52.                 return _head;  
  53.             }  
  54.         }  
  55.   
  56.   
  57.         private List<Unit> data = new List<Unit>();  
  58.   
  59.         [XmlArray("data")]  
  60.         [XmlArrayItem("unit")]  
  61.         public List<Unit> Unit  
  62.         {  
  63.             get { return data; }  
  64.         }  
  65.         public void addUnit(Unit e)  
  66.         {  
  67.             data.Add(e);  
  68.         }  
  69.   
  70.         private string _msg;  
  71.   
  72.         public string msg  
  73.         {  
  74.             get { return _msg; }  
  75.             set { _msg = value; }  
  76.         }  
  77.   
  78.   
  79.     }  
  80. }  
using System.Collections.Generic;
using System.Xml.Serialization;

namespace XStream
{
    /// <summary>
    /// <creator>zhangdapeng</creator>
    /// </summary>
    [XmlRoot("skycenter")]
    public class AdsbEntity
    {
        private string _type;

        [XmlAttribute("type")]
        public string Type
        {
            get { return _type; }
            set { _type = value; }
        }
        private string _source;
        [XmlAttribute("source")]
        public string Source
        {
            get { return _source; }
            set { _source = value; }
        }
        private string _reality;
        [XmlAttribute("reality")]
        public string Reality
        {
            get { return _reality; }
            set { _reality = value; }
        }
        private string _rcvTime;
        [XmlAttribute("rcvTime")]
        public string RcvTime
        {
            get { return _rcvTime; }
            set { _rcvTime = value; }
        }
        private Head _head;
        //属性的定义
        [XmlElement("head")]
        public Head Head
        {
            set   //设定属性
            {
                _head = value;
            }
            get    //从属性获取值
            {
                return _head;
            }
        }


        private List<Unit> data = new List<Unit>();

        [XmlArray("data")]
        [XmlArrayItem("unit")]
        public List<Unit> Unit
        {
            get { return data; }
        }
        public void addUnit(Unit e)
        {
            data.Add(e);
        }

        private string _msg;

        public string msg
        {
            get { return _msg; }
            set { _msg = value; }
        }


    }
}
  1. using System.Xml.Serialization;  
  2.   
  3. namespace XStream  
  4. {  
  5.     /// <summary>  
  6.     /// <creator>zhangdapeng</creator>  
  7.     /// </summary>  
  8.     public class Head  
  9.     {  
  10.          
  11.         private string _datagramId;  
  12.          [XmlElement("datagramId")]  
  13.         public string DatagramId  
  14.         {  
  15.             set  
  16.             {  
  17.                 _datagramId = value;  
  18.             }  
  19.             get  
  20.             {  
  21.                 return _datagramId;  
  22.             }  
  23.         }  
  24.   
  25.         private string _priority;  
  26.           [XmlElement("priority")]  
  27.         public string Priority  
  28.         {  
  29.             get { return _priority; }  
  30.             set { _priority = value; }  
  31.         }  
  32.   
  33.         private string _an;  
  34.          [XmlElement("an")]  
  35.         public string An  
  36.         {  
  37.             get { return _an; }  
  38.             set { _an = value; }  
  39.         }  
  40.   
  41.         private string _fi;  
  42.          [XmlElement("fi")]  
  43.         public string Fi  
  44.         {  
  45.             get { return _fi; }  
  46.             set { _fi = value; }  
  47.         }  
  48.   
  49.         private string _rcvAddress;  
  50.          [XmlElement("rcvAddress")]  
  51.         public string RcvAddress  
  52.         {  
  53.             get { return _rcvAddress; }  
  54.             set { _rcvAddress = value; }  
  55.         }  
  56.   
  57.         private string _sndAddress;  
  58.         [XmlElement("sndAddress")]  
  59.         public string SndAddress  
  60.         {  
  61.             get { return _sndAddress; }  
  62.             set { _sndAddress = value; }  
  63.         }  
  64.   
  65.         private string _bepTime;  
  66.         [XmlElement("bepTime")]  
  67.         public string BepTime  
  68.         {  
  69.             get { return _bepTime; }  
  70.             set { _bepTime = value; }  
  71.         }  
  72.   
  73.         private string _smi;  
  74.         [XmlElement("smi")]  
  75.         public string Smi  
  76.         {  
  77.             get { return _smi; }  
  78.             set { _smi = value; }  
  79.         }  
  80.   
  81.         private string _dsp;  
  82.          [XmlElement("dsp")]  
  83.         public string Dsp  
  84.         {  
  85.             get { return _dsp; }  
  86.             set { _dsp = value; }  
  87.         }  
  88.   
  89.         private string _rgs;  
  90.          [XmlElement("rgs")]  
  91.         public string Rgs  
  92.         {  
  93.             get { return _rgs; }  
  94.             set { _rgs = value; }  
  95.         }  
  96.   
  97.         private string _msn;  
  98.          [XmlElement("msn")]  
  99.         public string Msn  
  100.         {  
  101.             get { return _msn; }  
  102.             set { _msn = value; }  
  103.         }  
  104.   
  105.         private string _datagramType;  
  106.          [XmlElement("datagramType")]  
  107.         public string DatagramType  
  108.         {  
  109.             get { return _datagramType; }  
  110.             set { _datagramType = value; }  
  111.         }  
  112.   
  113.         private string _icao24;  
  114.          [XmlElement("icao24")]  
  115.         public string Icao24  
  116.         {  
  117.             get { return _icao24; }  
  118.             set { _icao24 = value; }  
  119.         }  
  120.   
  121.         private string _callcode;  
  122.         [XmlElement("callcode")]  
  123.         public string Callcode  
  124.         {  
  125.             get { return _callcode; }  
  126.             set { _callcode = value; }  
  127.         }  
  128.   
  129.   
  130.   
  131.     }  
  132. }  
using System.Xml.Serialization;

namespace XStream
{
    /// <summary>
    /// <creator>zhangdapeng</creator>
    /// </summary>
    public class Head
    {
       
        private string _datagramId;
         [XmlElement("datagramId")]
        public string DatagramId
        {
            set
            {
                _datagramId = value;
            }
            get
            {
                return _datagramId;
            }
        }

        private string _priority;
          [XmlElement("priority")]
        public string Priority
        {
            get { return _priority; }
            set { _priority = value; }
        }

        private string _an;
         [XmlElement("an")]
        public string An
        {
            get { return _an; }
            set { _an = value; }
        }

        private string _fi;
         [XmlElement("fi")]
        public string Fi
        {
            get { return _fi; }
            set { _fi = value; }
        }

        private string _rcvAddress;
         [XmlElement("rcvAddress")]
        public string RcvAddress
        {
            get { return _rcvAddress; }
            set { _rcvAddress = value; }
        }

        private string _sndAddress;
        [XmlElement("sndAddress")]
        public string SndAddress
        {
            get { return _sndAddress; }
            set { _sndAddress = value; }
        }

        private string _bepTime;
        [XmlElement("bepTime")]
        public string BepTime
        {
            get { return _bepTime; }
            set { _bepTime = value; }
        }

        private string _smi;
        [XmlElement("smi")]
        public string Smi
        {
            get { return _smi; }
            set { _smi = value; }
        }

        private string _dsp;
         [XmlElement("dsp")]
        public string Dsp
        {
            get { return _dsp; }
            set { _dsp = value; }
        }

        private string _rgs;
         [XmlElement("rgs")]
        public string Rgs
        {
            get { return _rgs; }
            set { _rgs = value; }
        }

        private string _msn;
         [XmlElement("msn")]
        public string Msn
        {
            get { return _msn; }
            set { _msn = value; }
        }

        private string _datagramType;
         [XmlElement("datagramType")]
        public string DatagramType
        {
            get { return _datagramType; }
            set { _datagramType = value; }
        }

        private string _icao24;
         [XmlElement("icao24")]
        public string Icao24
        {
            get { return _icao24; }
            set { _icao24 = value; }
        }

        private string _callcode;
        [XmlElement("callcode")]
        public string Callcode
        {
            get { return _callcode; }
            set { _callcode = value; }
        }



    }
}
  1. using System.Xml.Serialization;  
  2.   
  3. namespace XStream  
  4. {  
  5.     /// <summary>  
  6.     /// <creator>zhangdapeng</creator>  
  7.     /// </summary>  
  8.       [XmlRootAttribute("unit")]  
  9.     public class Unit  
  10.     {  
  11.         private string _name;  
  12.   
  13.           [XmlAttribute("name")]  
  14.         public string Name  
  15.         {  
  16.             get { return _name; }  
  17.             set { _name = value; }  
  18.         }  
  19.             
  20.         private string _name_value;  
  21.   
  22.          [XmlTextAttribute]  
  23.         public string Name_value  
  24.         {  
  25.             get { return _name_value; }  
  26.             set { _name_value = value; }  
  27.         }  
  28.     }  
  29. }  
using System.Xml.Serialization;

namespace XStream
{
    /// <summary>
    /// <creator>zhangdapeng</creator>
    /// </summary>
      [XmlRootAttribute("unit")]
    public class Unit
    {
        private string _name;

          [XmlAttribute("name")]
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
          
        private string _name_value;

         [XmlTextAttribute]
        public string Name_value
        {
            get { return _name_value; }
            set { _name_value = value; }
        }
    }
}

主类:
  1. using System;  
  2. using System.Xml;  
  3.   
  4. namespace XStream  
  5. {  
  6.     /// <summary>  
  7.     /// <creator>zhangdapeng</creator>  
  8.     /// </summary>  
  9.     class Program  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.              
  14.   
  15.             string path = System.AppDomain.CurrentDomain.BaseDirectory;  
  16.             XmlDocument xmlDoc = new XmlDocument();  
  17.       
  18.             xmlDoc.Load(@path+"../../test.xml");  
  19.             Console.WriteLine(xmlDoc.OuterXml);  
  20.   
  21.             AdsbEntity ad = XmlSerializeUtil.Deserialize(typeof(AdsbEntity), xmlDoc.OuterXml) as AdsbEntity;  
  22.             Console.WriteLine(ad.Head.DatagramId);  
  23.             Console.WriteLine(ad.Head.Fi);  
  24.   
  25.             string xml = XmlSerializeUtil.Serializer(typeof(AdsbEntity), ad);  
  26.             Console.WriteLine(xml);  
  27.               
  28.             Console.ReadKey();  
  29.   
  30.         }  
  31.     }  
  32. }  
using System;
using System.Xml;

namespace XStream
{
    /// <summary>
    /// <creator>zhangdapeng</creator>
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
           

            string path = System.AppDomain.CurrentDomain.BaseDirectory;
            XmlDocument xmlDoc = new XmlDocument();
    
            xmlDoc.Load(@path+"../../test.xml");
            Console.WriteLine(xmlDoc.OuterXml);

            AdsbEntity ad = XmlSerializeUtil.Deserialize(typeof(AdsbEntity), xmlDoc.OuterXml) as AdsbEntity;
            Console.WriteLine(ad.Head.DatagramId);
            Console.WriteLine(ad.Head.Fi);

            string xml = XmlSerializeUtil.Serializer(typeof(AdsbEntity), ad);
            Console.WriteLine(xml);
            
            Console.ReadKey();

        }
    }
}

 

原始输入:test.xml文件

 

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <skycenter type="ADSBResult" source="ADSB" reality="T" rcvTime="2014-02-25 07:23:31">  
  3.   <head>  
  4.     <!--报文id 注:空节点-->  
  5.     <datagramId>127</datagramId>  
  6.     <!--priority 注:空节点-->  
  7.     <priority>18</priority>  
  8.     <!--机尾号-->  
  9.     <an>B-5662</an>  
  10.     <!--航班号-->  
  11.     <fi>HU7782</fi>  
  12.     <!--接收地址 注:空节点-->  
  13.     <rcvAddress>1</rcvAddress>  
  14.     <!--发送地址 注:空节点-->  
  15.     <sndAddress>1</sndAddress>  
  16.     <!--bepTime 注:空节点-->  
  17.     <bepTime>1</bepTime>  
  18.     <!--SMI 注:空节点-->  
  19.     <smi>1</smi>  
  20.     <!--DSP服务商 注:空节点-->  
  21.     <dsp>1</dsp>  
  22.     <!--地面站-->  
  23.     <rgs>PEK</rgs>  
  24.     <!--流水号 注:空节点-->  
  25.     <msn>1</msn>  
  26.     <!--报文类型,都是POS 注:空节点-->  
  27.     <datagramType>1</datagramType>  
  28.     <!--ICAO24位地址码-->  
  29.     <icao24>7808a3</icao24>  
  30.     <!--呼号-->  
  31.     <callcode>CHH7782</callcode>  
  32.   </head>  
  33.   <data>  
  34.     <!--纬度 看前面的字母“N”或“S”,格式“DDMMSS.SS”-->  
  35.     <unit name="LAT">N31414709</unit>  
  36.     <!--经度 看前面的字母“E”或“W”,格式“DDDMMSS.SS”-->  
  37.     <unit name="LON">E118463546</unit>  
  38.     <!--气压高度,单位是米-->  
  39.     <unit name="ALT">396.24</unit>  
  40.     <!--计算空速 单位海里每小时-->  
  41.     <unit name="CAS">127.00</unit>  
  42.     <!--航向   正北是0°,顺时针,0到360°-->  
  43.     <unit name="VEC">58.18</unit>  
  44.     <!--收到报文的UTC时间 hh24mmss-->  
  45.     <unit name="UTC">072331</unit>  
  46.   </data>  
  47.   <msg>11</msg>  
  48. </skycenter>  
<?xml version="1.0" encoding="UTF-8"?>
<skycenter type="ADSBResult" source="ADSB" reality="T" rcvTime="2014-02-25 07:23:31">
  <head>
    <!--报文id 注:空节点-->
    <datagramId>127</datagramId>
    <!--priority 注:空节点-->
    <priority>18</priority>
    <!--机尾号-->
    <an>B-5662</an>
    <!--航班号-->
    <fi>HU7782</fi>
    <!--接收地址 注:空节点-->
    <rcvAddress>1</rcvAddress>
    <!--发送地址 注:空节点-->
    <sndAddress>1</sndAddress>
    <!--bepTime 注:空节点-->
    <bepTime>1</bepTime>
    <!--SMI 注:空节点-->
    <smi>1</smi>
    <!--DSP服务商 注:空节点-->
    <dsp>1</dsp>
    <!--地面站-->
    <rgs>PEK</rgs>
    <!--流水号 注:空节点-->
    <msn>1</msn>
    <!--报文类型,都是POS 注:空节点-->
    <datagramType>1</datagramType>
    <!--ICAO24位地址码-->
    <icao24>7808a3</icao24>
    <!--呼号-->
    <callcode>CHH7782</callcode>
  </head>
  <data>
    <!--纬度 看前面的字母“N”或“S”,格式“DDMMSS.SS”-->
    <unit name="LAT">N31414709</unit>
    <!--经度 看前面的字母“E”或“W”,格式“DDDMMSS.SS”-->
    <unit name="LON">E118463546</unit>
    <!--气压高度,单位是米-->
    <unit name="ALT">396.24</unit>
    <!--计算空速 单位海里每小时-->
    <unit name="CAS">127.00</unit>
    <!--航向   正北是0°,顺时针,0到360°-->
    <unit name="VEC">58.18</unit>
    <!--收到报文的UTC时间 hh24mmss-->
    <unit name="UTC">072331</unit>
  </data>
  <msg>11</msg>
</skycenter>


结果:

 

 

  1. </pre><pre class="html" name="code"><?xml version="1.0" encoding="UTF-8"?>  
  2. <skycenter type="ADSBResult" source="ADSB" reality="T" rcvTime="2014-02-25 07:23:31">  
  3.   <head>

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#线程中更新界面发布时间:2022-07-13
下一篇:
C#获取局域网IP和MAC地址发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap