序列化是把一個內存中的對象的信息轉化成一個可以持久化保存的形式,以便於保存或傳輸,序列化的主要作用是不同平台之間進行通信,常用的有序列化有json、xml、文件等,下面就逐個講下這三種序列化的方法。 一、序列化為json C#中用於對象和json相互轉換的原生類有兩個:DataContractJsonSerializer和JavaScriptSerializer,其中JavaScriptSerializer主要用於web的浏覽器和服務器之間的通信。這裡主要講DataContractJsonSerializer的使用,要使用DataContractJsonSerializer,先要在項目中引用System.Runtime.Serialization。首先准備一個測試的類Book: 復制代碼 1 [DataContract] 2 class Book 3 { 4 [DataMember] 5 public int ID { get; set; } 6 7 [DataMember] 8 public string Name { get; set; } 9 10 [DataMember] 11 public float Price { get; set; } 12 } 復制代碼 [DataContract]指定該類型要定義或實現一個數據協定,並可由序列化程序(如 System.Runtime.Serialization.DataContractSerializer)進行序列化。 [DataMember]當應用於類型的成員時,指定該成員是數據協定的一部分並可由 System.Runtime.Serialization.DataContractSerializer進行序列化。 然後先創建一個Book對象,實例化一個DataContractJsonSerializer實例,最後用該實例的WriteObject()方法將對象寫到流中,代碼如下: 1 class Program 2 { 3 static void Main(string[] args) 4 { 5 Book book = new Book() { ID = 101, Name = "C#程序設計", Price = 79.5f }; 6 7 //序列化json 8 DataContractJsonSerializer formatter= new DataContractJsonSerializer(typeof(Book)); 9 using (MemoryStream stream = new MemoryStream()) 10 { 11 formatter.WriteObject(stream, book); 12 string result = System.Text.Encoding.UTF8.GetString(stream.ToArray()); 13 Console.WriteLine(result); 14 } 15 } 16 } 將一個json格式的字符串反序列化為對象是用DataContractJsonSerializer實例的ReadObject()方法,代碼如下: 復制代碼 1 class Program 2 { 3 static void Main(string[] args) 4 { 5 Book book = new Book() { ID = 101, Name = "C#程序設計", Price = 79.5f };15 6 //反序列化json 7 string oriStr = "{\"ID\":101,\"Name\":\"C#程序設計\",\"Price\":79.5}"; 8 DataContractJsonSerializer formatter = new DataContractJsonSerializer(typeof(Book)); 9 using (MemoryStream stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(oriStr))) 10 { 11 Book outBook = formatter.ReadObject(stream) as Book; 12 Console.WriteLine(outBook.ID); 13 Console.WriteLine(outBook.Name); 14 Console.WriteLine(outBook.Price); 15 } 16 Console.Read(); 17 } 18 } 我們也可以把上面的json序列化與反序列為封裝成泛型方法,這樣可以公用,全部代碼如下: 復制代碼 1 class Program 2 { 3 static void Main(string[] args) 4 { 5 Book book = new Book() { ID = 101, Name = "C#程序設計", Price = 79.5f }; 6 7 //序列化json 8 string result = Serializer.ObjectToJson<Book>(book); 9 Console.WriteLine(result); 10 11 //反序列化json 12 string oriStr = "{\"ID\":101,\"Name\":\"C#程序設計\",\"Price\":79.5}"; 13 Book outBook = Serializer.JsonToObject<Book>(oriStr); 14 Console.WriteLine(outBook.ID); 15 Console.WriteLine(outBook.Name); 16 Console.WriteLine(outBook.Price); 17 18 Console.Read(); 19 } 20 } 21 22 public class Serializer 23 { 24 /// 將對象序列化為json文件 25 /// </summary> 26 /// <typeparam name="T">類型</typeparam> 27 /// <param name="t">實例</param> 28 /// <param name="path">存放路徑</param> 29 public static void ObjectToJson<T>(T t, string path) where T : class 30 { 31 DataContractJsonSerializer formatter = new DataContractJsonSerializer(typeof(T)); 32 using (FileStream stream = new FileStream(path, FileMode.OpenOrCreate)) 33 { 34 formatter.WriteObject(stream, t); 35 } 36 } 37 38 /// <summary> 39 /// 將對象序列化為json字符串 40 /// </summary> 41 /// <typeparam name="T">類型</typeparam> 42 /// <param name="t">實例</param> 43 /// <returns>json字符串</returns> 44 public static string ObjectToJson<T>(T t) where T : class 45 { 46 DataContractJsonSerializer formatter = new DataContractJsonSerializer(typeof(T)); 47 using (MemoryStream stream = new MemoryStream()) 48 { 49 formatter.WriteObject(stream, t); 50 string result = System.Text.Encoding.UTF8.GetString(stream.ToArray()); 51 return result; 52 } 53 } 54 55 /// <summary> 56 /// json字符串轉成對象 57 /// </summary> 58 /// <typeparam name="T">類型</typeparam> 59 /// <param name="json">json格式字符串</param> 60 /// <returns>對象</returns> 61 public static T JsonToObject<T>(string json) where T : class 62 { 63 DataContractJsonSerializer formatter = new DataContractJsonSerializer(typeof(Book)); 64 using (MemoryStream stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(json))) 65 { 66 T result = formatter.ReadObject(stream) as T; 67 return result; 68 } 69 } 70 } 復制代碼 二、序列化為xml C#中將對象序列化和反序列化為xml的類是XmlSerializer,要引用System.Xml.Serialization 先創建一個XmlSerializer對象實例,然後用實例的Serialize的方法將對象寫入到文件流中,代碼如下: 復制代碼 1 class Program 2 { 3 static void Main(string[] args) 4 { 5 Book book = new Book() { ID = 101, Name = "C#程序設計", Price = 79.5f }; 6 7 XmlSerializer formatter = new XmlSerializer(typeof(Book)); 8 using (FileStream stream = new FileStream(@"c:\book.xml", FileMode.OpenOrCreate)) 9 { 10 formatter.Serialize(stream, book); 11 } 12 Console.Read(); 13 } 14 } 復制代碼 程序運行後會在c盤產生一個book.xml文件,內容如下: 當然也可以將對象轉換成對象流,然後轉換成xml格式的字符串,代碼和效果圖如下: 復制代碼 1 class Program 2 { 3 static void Main(string[] args) 4 { 5 Book book = new Book() { ID = 101, Name = "C#程序設計", Price = 79.5f }; 6 7 XmlSerializer formatter = new XmlSerializer(typeof(Book)); 8 using (MemoryStream stream = new MemoryStream()) 9 { 10 formatter.Serialize(stream, book); 11 string result = System.Text.Encoding.UTF8.GetString(stream.ToArray());//轉換成xml字符串 12 Console.WriteLine(result); 13 } 14 Console.Read(); 15 } 16 } 復制代碼 將xml文件反序列化的方法是用XmlSerializer實例的Deserialize()方法,代碼如下: 復制代碼 1 class Program 2 { 3 static void Main(string[] args) 4 { 5 Book book = new Book() { ID = 101, Name = "C#程序設計", Price = 79.5f }; 6 7 XmlSerializer formatter = new XmlSerializer(typeof(Book)); 8 using (FileStream stream = new FileStream(@"c:book.xml", FileMode.OpenOrCreate)) 9 { 10 XmlReader xmlReader = new XmlTextReader(stream); 11 Book outBook = formatter.Deserialize(xmlReader) as Book;//反序列化 12 Console.WriteLine(outBook.ID); 13 Console.WriteLine(outBook.Name); 14 Console.WriteLine(outBook.Price); 15 } 16 Console.Read(); 17 } 18 } 我們同樣也可以把上面的xml序列化與反序列為封裝成泛型方法,這樣可以公用,全部代碼如下: 復制代碼 1 class Program 2 { 3 static void Main(string[] args) 4 { 5 Book book = new Book() { ID = 101, Name = "C#程序設計", Price = 79.5f }; 6 7 //序列化xml 8 Serializer.ObjectToXml(book, @"c:\book.xml"); 9 10 //反序列化xml 11 Book outBook = Serializer.XmlToObject(book, @"c:\book.xml"); 12 Console.WriteLine(outBook.ID); 13 Console.WriteLine(outBook.Name); 14 Console.WriteLine(outBook.Price); 15 Console.Read(); 16 } 17 } 18 19 public class Serializer 20 { 21 /// <summary> 22 /// 將對象序列化為xml文件 23 /// </summary> 24 /// <typeparam name="T">類型</typeparam> 25 /// <param name="t">對象</param> 26 /// <param name="path">xml存放路徑</param> 27 public static void ObjectToXml<T>(T t,string path) where T : class 28 { 29 XmlSerializer formatter = new XmlSerializer(typeof(T)); 30 using (FileStream stream = new FileStream(path, FileMode.OpenOrCreate)) 31 { 32 formatter.Serialize(stream, t); 33 } 34 } 35 36 /// <summary> 37 /// 將對象序列化為xml字符串 38 /// </summary> 39 /// <typeparam name="T">類型</typeparam> 40 /// <param name="t">對象</param> 41 public static string ObjectToXml<T>(T t) where T : class 42 { 43 XmlSerializer formatter = new XmlSerializer(typeof(T)); 44 using (MemoryStream stream = new MemoryStream()) 45 { 46 formatter.Serialize(stream, t); 47 string result = System.Text.Encoding.UTF8.GetString(stream.ToArray()); 48 return result; 49 } 50 } 51 52 /// <summary> 53 /// 將xml文件反序列化為對象 54 /// </summary> 55 /// <typeparam name="T">類型</typeparam> 56 /// <param name="t">對象</param> 57 /// <param name="path">xml路徑</param> 58 /// <returns>對象</returns> 59 public static T XmlToObject<T>(T t, string path) where T : class 60 { 61 XmlSerializer formatter = new XmlSerializer(typeof(T)); 62 using (FileStream stream = new FileStream(path, FileMode.OpenOrCreate)) 63 { 64 XmlReader xmlReader = new XmlTextReader(stream); 65 T result = formatter.Deserialize(xmlReader) as T; 66 return result; 67 } 68 } 69 } 復制代碼 三、序列化為文件 C#中將對象序列化和反序列化為二進制文件的類是BinaryFormatter,要引用System.Runtime.Serialization.Formatters.Binary 先創建一個BinaryFormatter對象實例,然後用實例的Serialize的方法將對象寫入到文件流中,代碼如下: 復制代碼 1 class Program 2 { 3 static void Main(string[] args) 4 { 5 Book book = new Book() { ID = 101, Name = "C#程序設計", Price = 79.5f }; 6 7 //序列化文件 8 BinaryFormatter formatter = new BinaryFormatter(); 9 using (FileStream stream = new FileStream(@"c:\book.txt", FileMode.OpenOrCreate)) 10 { 11 formatter.Serialize(stream, book); 12 } 13 Console.Read(); 14 } 15 } 復制代碼 程序執行完成後產生bool.txt文件,如圖: 可以通過BinaryFormatter類型實例的Deserialize()方法把二進制文本反序列化為對象,代碼如下: 復制代碼 1 class Program 2 { 3 static void Main(string[] args) 4 { 5 Book book = new Book() { ID = 101, Name = "C#程序設計", Price = 79.5f }; 6 7 //序列化文件 8 BinaryFormatter formatter = new BinaryFormatter(); 9 using (FileStream stream = new FileStream(@"c:\book.txt", FileMode.OpenOrCreate)) 10 { 11 Book outBook = formatter.Deserialize(stream) as Book; 12 Console.WriteLine(outBook.ID); 13 Console.WriteLine(outBook.Name); 14 Console.WriteLine(outBook.Price); 15 } 16 Console.Read(); 17 } 18 } 復制代碼 程序執行後顯示如圖: 我們同樣也可以把序列化和把序列化為二進制文件的方法封裝成泛型方法,全部代碼如下: 復制代碼 1 class Program 2 { 3 static void Main(string[] args) 4 { 5 Book book = new Book() { ID = 101, Name = "C#程序設計", Price = 79.5f }; 6 //序列化文件 7 Serializer.ObjectToFile(book, @"c:\book.txt"); 8 9 //反序列化文件 10 Book outBook = Serializer.FileToObject<Book>(@"c:\book.txt") as Book; 11 Console.WriteLine(outBook.ID); 12 Console.WriteLine(outBook.Name); 13 Console.WriteLine(outBook.Price); 14 Console.Read(); 15 } 16 } 17 18 public class Serializer 19 { 20 #region 文件序列化 21 /// <summary> 22 /// 將對象序列化為字符串 23 /// </summary> 24 /// <typeparam name="T">類型</typeparam> 25 /// <param name="t">實例</param> 26 /// <returns>字符串</returns> 27 public static string ObjectToString<T>(T t) 28 { 29 BinaryFormatter formatter = new BinaryFormatter(); 30 using (MemoryStream stream = new MemoryStream()) 31 { 32 formatter.Serialize(stream, t); 33 string result = System.Text.Encoding.UTF8.GetString(stream.ToArray()); 34 return result; 35 } 36 } 37 38 /// <summary> 39 /// 將對象序列化為文件 40 /// </summary> 41 /// <typeparam name="T">類型</typeparam> 42 /// <param name="t">實例</param> 43 /// <param name="path">存放路徑</param> 44 public static void ObjectToFile<T>(T t, string path) 45 { 46 BinaryFormatter formatter = new BinaryFormatter(); 47 using (FileStream stream = new FileStream(path, FileMode.OpenOrCreate)) 48 { 49 formatter.Serialize(stream, t); 50 stream.Flush(); 51 } 52 } 53 54 /// <summary> 55 /// 將字符串反序列為類型 56 /// </summary> 57 /// <typeparam name="T">類型</typeparam> 58 /// <param name="s">字符串</param> 59 /// <returns>對象</returns> 60 public static T StringToObject<T>(string s) where T : class 61 { 62 byte[] buffer = System.Text.Encoding.UTF8.GetBytes(s); 63 BinaryFormatter formatter = new BinaryFormatter(); 64 using (MemoryStream stream = new MemoryStream(buffer)) 65 { 66 T result = formatter.Deserialize(stream) as T; 67 return result; 68 } 69 } 70 71 /// <summary> 72 /// 將文件反序列化為對象 73 /// </summary> 74 /// <typeparam name="T">類型</typeparam> 75 /// <param name="path">路徑</param> 76 /// <returns>對象</returns> 77 public static T FileToObject<T>(string path) where T : class 78 { 79 using (FileStream stream = new FileStream(path, FileMode.Open)) 80 { 81 BinaryFormatter formatter = new BinaryFormatter(); 82 T result = formatter.Deserialize(stream) as T; 83 return result; 84 } 85 } 86 #endregion 87 } 88 89 [Serializable] 90 public class Book 91 { 92 public int ID { get; set; } 93 public string Name { get; set; } 94 public float Price { get; set; } 95 } 復制代碼