C# XML反序列化与序列化举例:XmlSerializer(转)
生活随笔
收集整理的這篇文章主要介紹了
C# XML反序列化与序列化举例:XmlSerializer(转)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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 } View Code
?
這個類是個實體類
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; }}} } View Code 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 } View Code 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 } View Code
主類:
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 } View Code
原始輸入: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> View Code
結果:
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> 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> 49 50 127 51 HU7782 52 <?xml version="1.0"?> 53 <skycenter xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http 54 ://www.w3.org/2001/XMLSchema" type="ADSBResult" source="ADSB" reality="T" rcvTim 55 e="2014-02-25 07:23:31"> 56 <head> 57 <datagramId>127</datagramId> 58 <priority>18</priority> 59 <an>B-5662</an> 60 <fi>HU7782</fi> 61 <rcvAddress>1</rcvAddress> 62 <sndAddress>1</sndAddress> 63 <bepTime>1</bepTime> 64 <smi>1</smi> 65 <dsp>1</dsp> 66 <rgs>PEK</rgs> 67 <msn>1</msn> 68 <datagramType>1</datagramType> 69 <icao24>7808a3</icao24> 70 <callcode>CHH7782</callcode> 71 </head> 72 <data> 73 <unit name="LAT">N31414709</unit> 74 <unit name="LON">E118463546</unit> 75 <unit name="ALT">396.24</unit> 76 <unit name="CAS">127.00</unit> 77 <unit name="VEC">58.18</unit> 78 <unit name="UTC">072331</unit> 79 </data> 80 <msg>11</msg> 81 </skycenter> View Code?
?原文鏈接:https://blog.csdn.net/philosophyatmath/article/details/28593153
轉載于:https://www.cnblogs.com/gdutzhulm/p/9891325.html
總結
以上是生活随笔為你收集整理的C# XML反序列化与序列化举例:XmlSerializer(转)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: postMan下使用xdebug
- 下一篇: 浅析几种常见算法(转载)