---------------------------------------------------------------文章1---------------------------------------------------------------ui
使用XmlSerializer進行串行化
關於格式化器還有一個問題,假設咱們須要XML,有兩中方案:要麼編寫一個實現IFormatter接口的類,採用的方式相似於SoapFormatter類,可是沒有你不須要的信息;要麼使用庫類XmlSerializer,這個類不使用Serializable屬性,可是它提供了相似的功能。
若是咱們不想使用主流的串行化機制,而想使用XmlSeralizer進行串行化咱們須要作一下修改:
a.添加System.Xml.Serialization命名空間。
b.Serializable和NoSerialized屬性將被忽略,而是使用XmlIgnore屬性,它的行爲與NoSerialized相似。
c.XmlSeralizer要求類有個默認的構造器,這個條件可能已經知足了。this
序列化:編碼
XmlSerializer xs = new XmlSerializer(typeof(List<Peoson>));
xs.Serialize(fs, listPers);spa
反序列化:.net
XmlSerializer xs = new XmlSerializer(typeof(List<Peoson>));
List<Peoson> list = xs.Deserialize(fs) as List<Peoson>;3d
using UnityEngine; using System; using System.Collections; using System.IO; using System.Xml.Serialization; using System.Xml; using System.Collections.Generic; [Serializable] // 表示該類能夠被序列化 [XmlRoot("AAA")] // 設置爲XML中的根元素名稱 public class Person { private string name; private int age; // [XmlElement("abc")] public int abc = 1000; // 類的public屬性字段均可以被序列化 // [XmlAttribute("Name")] 設置做爲xml中的屬性 public string Name { get { return name;} set { name = value;} } // [XmlElement("Age")] 設置做爲XML中的元素(默認狀態) public int Age { get { return age;} set { age = value;} } public Person() { } public Person(string name, int age) { this.name = name; this.age = age; } public void SayHi() { Debug.LogFormat ("我是{0}, 今年{1}歲", name, age); } } public class XmlSerialize : MonoBehaviour { string filePath = Directory.GetCurrentDirectory() + "/XmlFile.txt"; string filePath2 = Directory.GetCurrentDirectory() + "/XmlClassFile.txt"; // Use this for initialization void Start () { List<Person> listPers = new List<Person> (); Person per1 = new Person ("張三", 18); Person per2 = new Person ("李四", 20); listPers.Add (per1); listPers.Add (per2); SerializeMethod (listPers); // 序列化 DeserializeMethod(); // 反序列化 // SerializeClassMethod (per1); // DeserializeClassMethod (); Debug.Log("Done ! "); } void DeserializeClassMethod() // Xml實體類反序列化 { FileStream fs = new FileStream (filePath, FileMode.Open); XmlSerializer xs = new XmlSerializer(typeof(Person)); Person p = xs.Deserialize (fs) as Person; if (p != null) { p.SayHi (); } fs.Close (); } void SerializeClassMethod(Person p) // Xml實體類序列化 { FileStream fs = new FileStream (filePath2, FileMode.Create); XmlSerializer xs = new XmlSerializer(typeof(Person)); xs.Serialize(fs, p); fs.Close (); } void DeserializeMethod() // Xml列表反序列化 { FileStream fs = new FileStream (filePath, FileMode.Open); XmlSerializer xs = new XmlSerializer(typeof(List<Person>)); List<Person> listPers = xs.Deserialize (fs) as List<Person>; if (listPers != null) { for (int i = 0; i < listPers.Count; i++) { listPers [i].SayHi (); } } fs.Close (); } void SerializeMethod(List<Person> listPers) // Xml列表序列化 { FileStream fs = new FileStream (filePath, FileMode.Create); XmlSerializer xs = new XmlSerializer(typeof(List<Person>)); xs.Serialize(fs, listPers); fs.Close (); } // Update is called once per frame void Update () { } }
Xml列表序列化的內容:code
Xml實體類序列化的內容:orm
---------------------------------------------------------------文章2---------------------------------------------------------------xml
.Net Framework提供了對應的System.Xml.Seriazliation.XmlSerializer負責把對象序列化到XML,和從XML中反序列化爲對象。Serializer的使用比較直觀,須要多注意的是XML序列化相關的Attribute,怎麼把這些attribute應用到咱們的對象,以及對象公共屬性上面去,生成知足預期格式的XML。
本文列出了最經常使用的方法和特性,涵蓋平常大部分的轉換工做,但願你們在工做中快速上手。爲了給你們直觀的印象,這裏給出具體的使用代碼,爲了節省篇幅,代碼異常處理沒有添加,各位同窗使用的時候酌情添加。對象
1. Serializer方法
下面的方法封裝了XmlSerializer的調用,這裏列出了參數最全的一個版本,具體使用的時候需適當添加劇載:
public static class XmlSerializer { public static void SaveToXml(string filePath, object sourceObj, Type type, string xmlRootName) { if (!string.IsNullOrWhiteSpace(filePath) && sourceObj != null) { type = type != null ? type : sourceObj.GetType(); using (StreamWriter writer = new StreamWriter(filePath)) { System.Xml.Serialization.XmlSerializer xmlSerializer = string.IsNullOrWhiteSpace(xmlRootName) ? new System.Xml.Serialization.XmlSerializer(type) : new System.Xml.Serialization.XmlSerializer(type, new XmlRootAttribute(xmlRootName)); xmlSerializer.Serialize(writer, sourceObj); } } } public static object LoadFromXml(string filePath, Type type) { object result = null; if (File.Exists(filePath)) { using (StreamReader reader = new StreamReader(filePath)) { System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(type); result = xmlSerializer.Deserialize(reader); } } return result; } }
2. 序列化經常使用Attribute講解說明:
[XmlRootAttribute("MyCity", Namespace="abc.abc", IsNullable=false)] // 當該類爲Xml根節點時,以此爲根節點名稱。
public class City
[XmlAttribute("AreaName")] // 表現爲Xml節點屬性。<... AreaName="..."/>
public string Name
[XmlElementAttribute("AreaId", IsNullable = false)] // 表現爲Xml節點。<AreaId>...</AreaId>
public string Id
[XmlArrayAttribute("Areas")] // 表現爲Xml層次結構,根爲Areas,其所屬的每一個該集合節點元素名爲類名。<Areas><Area ... /><Area ... /></Areas>
public Area[] Areas
[XmlElementAttribute("Area", IsNullable = false)] // 表現爲水平結構的Xml節點。<Area ... /><Area ... />...
public Area[] Areas
[XmlIgnoreAttribute] // 忽略該元素的序列化。
3. 詳細舉例說明
這裏用簡單的城市,區域和街區做爲例子,具體示範一下上面的規則。
[XmlRootAttribute("MyCity", Namespace = "abc.abc", IsNullable = false)] public class City { [XmlAttribute("CityName")] public string Name { get; set; } [XmlAttribute("CityId")] public string Id { get; set; } [XmlArrayAttribute("Areas")] public Area[] Areas { get; set; } } [XmlRootAttribute("MyArea")] public class Area { [XmlAttribute("AreaName")] public string Name { get; set; } [XmlElementAttribute("AreaId", IsNullable = false)] public string Id { get; set; } [XmlElementAttribute("Street", IsNullable = false)] public string[] Streets { get; set; } }
根據以上類型,咱們mock一些數據,而後用步驟1給出的Util方法輸出:
static void Main(string[] args) { Area area1 = new Area(); area1.Name = "Pudong"; area1.Id = "PD001"; area1.Streets = new string [] { "street 001", "street 002" }; Area area2 = new Area(); area2.Name = "Xuhui"; area2.Id = "XH002"; area2.Streets = new string [] { "street 003", "street 004" }; City city1 = new City(); city1.Name = "Shanghai"; city1.Id = "SH001"; city1.Areas = new Area[] { area1, area2 }; XmlSerializer.SaveToXml(@"C:\temp\XML\output003.xml", city1); }
最終輸出的XML爲:
<?xml version="1.0" encoding="utf-8"?> <MyCity xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" CityName="Shanghai" CityId="SH001" xmlns="abc.abc"> <Areas> <Area AreaName="Pudong"> <AreaId>PD001</AreaId> <Street>street 001</Street> <Street>street 002</Street> </Area> <Area AreaName="Xuhui"> <AreaId>XH002</AreaId> <Street>street 003</Street> <Street>street 004</Street> </Area> </Areas> </MyCity>
下面咱們開始具體分析結果,其中包含一些頗有用的結論和注意事項:
1. xml的版本,編碼,以及命名空間xmlns:xsi,xmlns:xsd爲Framework自動添加。
2. 由於咱們用City對象做爲根節點,因此根節點名稱爲咱們定義的"MyCity"。
可是,注意!這裏指的是用City自身直接作根節點,若是是City集合好比City[],此時,該名稱失效,系統會自動生成名稱ArrayOfCity做爲根節點名稱(ArrayOf+類名),或者咱們手動指定名稱,這個就是在給你們的SaveToXml()方法中,參數xmlRootName的做用。
3. 若是以City爲根節點並在XmlRootAttribute特性中給定名稱,同時也手動指定了xmlRootName,系統會以手動指定的名稱爲準。
4. AreaName,AreaId,同爲Area類的公共屬性,一個被解釋成屬性,一個被解釋成子節點。
Areas集合被解釋成了層次結構,Streets集合被解釋成了水平結構。
這兩組區別最能體現不一樣序列化Attribute的用法
文章分別轉載自:https://blog.csdn.net/e295166319/article/details/5279127九、https://blog.csdn.net/zzy7075/article/details/50770062