C#中關于序列化與反序列化的三種方法
一.序列化與反序列化解釋
序列化 (Serialization)是將對象的狀態(tài)信息轉換為可以存儲或傳輸?shù)男问降倪^程。在序列化期間,對象將其當前狀態(tài)寫入到臨時或持久性存儲區(qū)。以后,可以通過從存儲區(qū)中讀取或反序列化對象的狀態(tài),重新創(chuàng)建該對象。
二.序列化目的
1、以某種存儲形式使自定義對象持久化;
2、將對象從一個地方傳遞到另一個地方;
3、使程序更具維護性等
三.C#中的三種序列化說明
1、以二進制格式序列化
二進制序列化保持類型保真度,這對于在應用程序的不同調用之間保留對象的狀態(tài)很有用。例如,通過將對象序列化到剪貼板,可在不同的應用程序之間共享對象。您可以將對象序列化到流、磁盤、內存和網(wǎng)絡等等。遠程處理使用序列化“通過值”在計算機或應用程序域之間傳遞對象。
引用命名空間:using System.Runtime.Serialization.Formatters.Binary;
2、以SOAP格式序列化
SOAP(Simple Object Access Protocol )簡單對象訪問協(xié)議是在分散或分布式的環(huán)境中交換信息的簡單的協(xié)議,是一個基于XML的協(xié)議,它包括四個部分:SOAP封裝(envelop),封裝定義了一個描述消息中的內容是什么,是誰發(fā)送的,誰應當接受并處理它以及如何處理它們的框架;SOAP編碼規(guī)則(encoding rules),用于表示應用程序需要使用的數(shù)據(jù)類型的實例; SOAP RPC表示(RPC representation),表示遠程過程調用和應答的協(xié)定;SOAP綁定(binding),使用底層協(xié)議交換信息。
引用命名空間:using System.Runtime.Serialization.Formatters.Soap;
3、將對象序列化到XML文檔
同SOAP也是保存成XML文件.但沒有其他額外信息。XML 序列化僅序列化public類型的字段(其他兩種類型能保存所有類型的字段),且不保持類型保真度,當您要提供或使用數(shù)據(jù)而不限制使用該數(shù)據(jù)的應用程序時,這一點是很有用的。由于 XML 是一個開放式標準,因此,對于通過 Web 共享數(shù)據(jù)而言,這是一個很好的選擇。SOAP 同樣是一個開放式標準,這使它也成為一個頗具吸引力的選擇。
引用命名空間:using System.Xml.Serialization;
三.C#中的三種序列化代碼示例
using System; using System.Windows.Forms; using System.IO; using System.Runtime.Serialization.Formatters.Binary;//二進制序列化 using System.Runtime.Serialization.Formatters.Soap;//SOAP序列化 using System.Xml.Serialization;//XML序列化 namespace WindowsFormsApplication1 { ? ? public partial class Form1 : Form ? ? { ? ? ? ? public Form1() ? ? ? ? { ? ? ? ? ? ? InitializeComponent(); ? ? ? ? } ? ? ? ? //如果要想保存某個class中的字段,必須在class前面加個這樣[Serializable] ? ? ? ? [Serializable] ? ? ? ? public class Person ? ? ? ? { ? ? ? ? ? ? public int age; ? ? ? ? ? ? public string name; ? ? ? ? ? ? [NonSerialized] //如果某個字段不想被保存,則加個這樣的標志 ? ? ? ? ? ? public string secret; ? ? ? ? } ? ? ? ? //序列化 ? ? ? ? private void btnXlh_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? Person person = new Person(); ? ? ? ? ? ? person.age = 28; ? ? ? ? ? ? person.name = "Hoam\r\n"; ? ? ? ? ? ? person.secret = "no look"; ? ? ? ? ? ? string str = System.AppDomain.CurrentDomain.BaseDirectory; ? ? ? ? ? ? //二進制序列化 ? ? ? ? ? ? FileStream streamHx = new FileStream(str+"personHx.txt", FileMode.Create); ? ? ? ? ? ? BinaryFormatter bFormatHx = new BinaryFormatter(); ? ? ? ? ? ? bFormatHx.Serialize(streamHx, person); ? ? ? ? ? ? streamHx.Close(); ? ? ? ? ? ? //SOAP序列化 ? ? ? ? ? ? FileStream streamSoap = new FileStream(str + "personSoap.xml", FileMode.Create); ? ? ? ? ? ? SoapFormatter bFormatSoap = new SoapFormatter(); ? ? ? ? ? ? bFormatSoap.Serialize(streamSoap, person); ? ? ? ? ? ? streamSoap.Close(); ? ? ? ? ? ? //XML序列化 ?//某個字段加[NonSerialized]標志不起作用,仍被保存 ? ? ? ? ? ? FileStream streamXml = new FileStream(str+ "personXml.xml", FileMode.Create); ? ? ? ? ? ? XmlSerializer xmlserilize = new XmlSerializer(typeof(Person)); ? ? ? ? ? ? xmlserilize.Serialize(streamXml, person); ? ? ? ? ? ? streamXml.Close(); ? ? ? ? } ? ? ? ? //反序列化 ? ? ? ? private void btnFxlh_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? Person person = new Person(); ? ? ? ? ? ? string str = System.AppDomain.CurrentDomain.BaseDirectory; ? ? ? ? ? ? object data; ? ? ? ? ? ? //二進制反序列化 ? ? ? ? ? ? FileStream streamHx = new FileStream(str + "personHx.txt", FileMode.OpenOrCreate); ? ? ? ? ? ? BinaryFormatter bFormatHx = new BinaryFormatter(); ? ? ? ? ? ? data = bFormatHx.Deserialize(streamHx); //反序列化得到的是一個object對象.須做下類型轉換 ? ? ? ? ? ? streamHx.Close(); ? ? ? ? ? ? //SOAP反序列化 ? ? ? ? ? ? FileStream streamSoap = new FileStream(str + "personSoap.xml", FileMode.OpenOrCreate); ? ? ? ? ? ? SoapFormatter bFormatSoap = new SoapFormatter(); ? ? ? ? ? ? data = bFormatSoap.Deserialize(streamSoap); //反序列化得到的是一個object對象.須做下類型轉換 ? ? ? ? ? ? streamSoap.Close(); ? ? ? ? ? ? //XML反序列化? ? ? ? ? ? ? FileStream streamXml = new FileStream(str + "personXml.xml", FileMode.OpenOrCreate); ? ? ? ? ? ? XmlSerializer xmlserilize = new XmlSerializer(typeof(Person)); ? ? ? ? ? ? data = xmlserilize.Deserialize(streamXml); //反序列化得到的是一個object對象.須做下類型轉換 ? ? ? ? ? ? streamXml.Close(); ? ? ? ? } ? ? } }
到此這篇關于C#中關于序列化與反序列化的三種方法的文章就介紹到這了,更多相關C# 序列化與反序列化內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
openfiledialog讀取txt寫入數(shù)據(jù)庫示例
這篇文章主要介紹了openfiledialog讀取txt寫入數(shù)據(jù)庫示例,需要的朋友可以參考下2014-03-03DevExpress實現(xiàn)GridControl根據(jù)列選中一行
這篇文章主要介紹了DevExpress實現(xiàn)GridControl根據(jù)列選中一行,比較實用的功能,需要的朋友可以參考下2014-08-08c#?Task.Wait()與awaiat?Task異常處理的區(qū)別說明
這篇文章主要介紹了c#?Task.Wait()與awaiat?Task異常處理的區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06