C# XML反序列化与序列化举例:XmlSerializer
- 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
- }
- }
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 } }
这个类是个实体类
- 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; }
- }
- }
- }
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; } } } }
- 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; }
- }
- }
- }
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; } } } }
- 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; }
- }
- }
- }
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; } } } }
主类:
- 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();
- }
- }
- }
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文件
- <?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>
<?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>
结果:
- </pre><pre class="html" name="code"><?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>
- 127
- HU7782
- <?xml version="1.0"?>
- <skycenter xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http
- ://www.w3.org/2001/XMLSchema" type="ADSBResult" source="ADSB" reality="T" rcvTim
- e="2014-02-25 07:23:31">
- <head>
- <datagramId>127</datagramId>
- <priority>18</priority>
- <an>B-5662</an>
- <fi>HU7782</fi>
- <rcvAddress>1</rcvAddress>
- <sndAddress>1</sndAddress>
- <bepTime>1</bepTime>
- <smi>1</smi>
- <dsp>1</dsp>
- <rgs>PEK</rgs>
- <msn>1</msn>
- <datagramType>1</datagramType>
- <icao24>7808a3</icao24>
- <callcode>CHH7782</callcode>
- </head>
- <data>
- <unit name="LAT">N31414709</unit>
- <unit name="LON">E118463546</unit>
- <unit name="ALT">396.24</unit>
- <unit name="CAS">127.00</unit>
- <unit name="VEC">58.18</unit>
- <unit name="UTC">072331</unit>
- </data>
- <msg>11</msg>
- </skycenter>
</pre><pre class="html" name="code"><?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> 127 HU7782 <?xml version="1.0"?> <skycenter xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http ://www.w3.org/2001/XMLSchema" type="ADSBResult" source="ADSB" reality="T" rcvTim e="2014-02-25 07:23:31"> <head> <datagramId>127</datagramId> <priority>18</priority> <an>B-5662</an> <fi>HU7782</fi> <rcvAddress>1</rcvAddress> <sndAddress>1</sndAddress> <bepTime>1</bepTime> <smi>1</smi> <dsp>1</dsp> <rgs>PEK</rgs> <msn>1</msn> <datagramType>1</datagramType> <icao24>7808a3</icao24> <callcode>CHH7782</callcode> </head> <data> <unit name="LAT">N31414709</unit> <unit name="LON">E118463546</unit> <unit name="ALT">396.24</unit> <unit name="CAS">127.00</unit> <unit name="VEC">58.18</unit> <unit name="UTC">072331</unit> </data> <msg>11</msg> </skycenter>
出处:http://blog.csdn.net/philosophyatmath/article/details/28593153
关注我】。(●'◡'●)
如果,您希望更容易地发现我的新博客,不妨点击一下绿色通道的【因为,我的写作热情也离不开您的肯定与支持,感谢您的阅读,我是【Jack_孟】!
本文来自博客园,作者:jack_Meng,转载请注明原文链接:https://www.cnblogs.com/mq0036/p/6390720.html
【免责声明】本文来自源于网络,如涉及版权或侵权问题,请及时联系我们,我们将第一时间删除或更改!
posted on 2017-02-12 12:11 jack_Meng 阅读(37663) 评论(0) 编辑 收藏 举报