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

