C# 的三种序列化方法
序列化是將一個(gè)對象轉(zhuǎn)換成字節(jié)流以達(dá)到將其長期保存在內(nèi)存、數(shù)據(jù)庫或文件中的處理過程。它的主要目的是保存對象的狀態(tài)以便以后需要的時(shí)候使用。與其相反的過程叫做反序列化。
序列化一個(gè)對象
為了序列化一個(gè)對象,我們需要一個(gè)被序列化的對象,一個(gè)容納被序列化了的對象的(字節(jié))流和一個(gè)格式化器。進(jìn)行序列化之前我們先看看System.Runtime.Serialization名字空間。ISerializable接口允許我們使任何類成為可序列化的類。
如果我們給自己寫的類標(biāo)識[Serializable]特性,我們就能將這些類序列化。除非類的成員標(biāo)記了[NonSerializable],序列化會將類中的所有成員都序列化。
序列化的類型
- 二進(jìn)制(流)序列化
- SOAP序列化
- XML序列化
二進(jìn)制(流)序列化:
二進(jìn)制(流)序列化是一種將數(shù)據(jù)寫到輸出流,以使它能夠用來自動重構(gòu)成相應(yīng)對象的機(jī)制。二進(jìn)制,其名字就暗示它的必要信息是保存在存儲介質(zhì)上,而這些必要信息要求創(chuàng)建一個(gè)對象的精確的二進(jìn)制副本。在二進(jìn)制(流)序列化中,整個(gè)對象的狀態(tài)都被保存起來,而XML序列化只有部分?jǐn)?shù)據(jù)被保存起來。為了使用序列化,我們需要引入System.Runtime.Serialization.Formatters.Binary名字空間. 下面的代碼使用BinaryFormatter類序列化.NET中的string類型的對象。
using System; using System.IO; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary;namespace SerializationTest {class Program{static void Main(string[] args){//Serialization of String Object string strobj = "test string for serialization";FileStream stream = new FileStream("C:\\StrObj.txt", FileMode.Create, FileAccess.Write ,FileShare.None);BinaryFormatter formatter = new BinaryFormatter();formatter.Serialize(stream, strobj);stream.Close();//Deserialization of String ObjectFileStream readstream = new FileStream("C:\\StrObj.txt", FileMode.Open , FileAccess.Read ,FileShare.Read );string readdata = (string)formatter.Deserialize(readstream);readstream.Close();Console.WriteLine(readdata);Console.ReadLine();}} }
SOAP序列化:
SOAP協(xié)議是一個(gè)在異構(gòu)的應(yīng)用程序之間進(jìn)行信息交互的理想的選擇。我們需要在應(yīng)用程序中添加System.Runtime.Serialization.Formatters.Soap名字空間以便在.Net中使用SOAP序列化。SOAP序列化的主要優(yōu)勢在于可移植性。SoapFormatter把對象序列化成SOAP消息或解析SOAP消息并重構(gòu)被序列化的對象。下面的代碼在.Net中使用SoapFormatter類序列化string類的對象。
using System; using System.IO; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Soap ;namespace SerializationTest{class Program{static void Main(string[] args){//Serialization of String Object string strobj = "test string for serialization";FileStream stream = new FileStream("C:\\StrObj.txt", FileMode.Create, FileAccess.Write ,FileShare.None);SoapFormatter formatter = new SoapFormatter();formatter.Serialize(stream, strobj);stream.Close();//Deserialization of String ObjectFileStream readstream = new FileStream("C:\\StrObj.txt", FileMode.Open , FileAccess.Read ,FileShare.Read );string readdata = (string)formatter.Deserialize(readstream);readstream.Close();Console.WriteLine(readdata);Console.ReadLine();}} }
XML序列化:
根據(jù)MSDN的描述,“XML序列化將一個(gè)對象或參數(shù)的公開字段和屬性以及方法的返回值轉(zhuǎn)換(序列化)成遵循XSD文檔標(biāo)準(zhǔn)的XML流。因?yàn)閄ML是一個(gè)開放的標(biāo)準(zhǔn),XML能被任何需要的程序處理,而不管在什么平臺下,因此XML序列化被用到帶有公開的屬性和字段的強(qiáng)類型類中,它的這些發(fā)生和字段被轉(zhuǎn)換成序列化的格式(在這里是XML)存儲或傳輸。”
我們必須添加System.XML.Serialization引用以使用XML序列化。使用XML序列化的基礎(chǔ)是XmlSerializer。下面的代碼是在.Net中使用XmlSerializer類序列化string對象。
using System; using System.IO; using System.Xml.Serialization;namespace SerializationTest {class Program{static void Main(string[] args){//Serialization of String Object string strobj = "test string for serialization";FileStream stream = new FileStream("C:\\StrObj.txt", FileMode.Create, FileAccess.Write ,FileShare.None);XmlSerializer xmlserializer = new XmlSerializer(typeof(string));xmlserializer.Serialize(stream, strobj);stream.Close();//Deserialization of String ObjectFileStream readstream = new FileStream("C:\\StrObj.txt", FileMode.Open , FileAccess.Read ,FileShare.Read );string readdata = (string)xmlserializer.Deserialize(readstream);readstream.Close();Console.WriteLine(readdata);Console.ReadLine();}} }
什么是格式化器?
一個(gè)格式化器用來確定一個(gè)對象的序列格式。它們目的是在網(wǎng)絡(luò)上傳輸一個(gè)對象之前將其序列化成合適的格式。它們提供IFormatter接口。在.NET里提供了兩個(gè)格式化類:BinaryFormatter和SoapFormatter,它們都繼承了IFormatter接口。
使用序列化
序列化允許開發(fā)人員保存一個(gè)對象的狀態(tài)并在需要的時(shí)候重構(gòu)對象,同時(shí)很好地支持對象存儲和數(shù)據(jù)交換。通過序列化,開發(fā)人員可以利用Web Service發(fā)送對象到遠(yuǎn)端應(yīng)用程序,從一個(gè)域傳輸對象到另一個(gè)域,以XML的格式傳輸一個(gè)對象并能通過防火墻,或者在應(yīng)用程序間保持安全性或用戶特定信息等等。
本文中的所有譯文僅用于學(xué)習(xí)和交流目的,轉(zhuǎn)載請務(wù)必注明文章譯者、出處、和本文鏈接
我們的翻譯工作遵照 CC 協(xié)議,如果我們的工作有侵犯到您的權(quán)益,請及時(shí)聯(lián)系我們
?
轉(zhuǎn)載于:https://www.cnblogs.com/rinack/p/3668873.html
總結(jié)
以上是生活随笔為你收集整理的C# 的三种序列化方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android Splash界面支持用户
- 下一篇: 做个脑部CT多少钱?