一.序列化與反序列化解釋
序列化 (Serialization)是將物件的狀態資訊轉換為可以存盤或傳輸的形式的程序,在序列化期間,物件將其當前狀態寫入到臨時或持久性存盤區,以后,可以通過從存盤區中讀取或反序列化物件的狀態,重新創建該物件,
二.序列化目的
1、以某種存盤形式使自定義物件持久化;
2、將物件從一個地方傳遞到另一個地方;
3、使程式更具維護性等三.C#中的三種序列化說明
1、以二進制格式序列化
二進制序列化保持型別保真度,這對于在應用程式的不同呼叫之間保留物件的狀態很有用,例如,通過將物件序列化到剪貼板,可在不同的應用程式之間共享物件,您可以將物件序列化到流、磁盤、記憶體和網路等等,遠程處理使用序列化“通過值”在計算機或應用程式域之間傳遞物件,
參考命名空間:using System.Runtime.Serialization.Formatters.Binary;
4、以SOAP格式序列化
SOAP(Simple Object Access Protocol )簡單物件訪問協議是在分散或分布式的環境中交換資訊的簡單的協議,是一個基于XML的協議,它包括四個部分:SOAP封裝(envelop),封裝定義了一個描述訊息中的內容是什么,是誰發送的,誰應當接受并處理它以及如何處理它們的框架;SOAP編碼規則(encoding rules),用于表示應用程式需要使用的資料型別的實體; SOAP RPC表示(RPC representation),表示遠程程序呼叫和應答的協定;SOAP系結(binding),使用底層協議交換資訊,
參考命名空間:using System.Runtime.Serialization.Formatters.Soap;
3、將物件序列化到XML檔案
同SOAP也是保存成XML檔案.但沒有其他額外資訊,XML 序列化僅序列化public型別的欄位(其他兩種型別能保存所有型別的欄位),且不保持型別保真度,當您要提供或使用資料而不限制使用該資料的應用程式時,這一點是很有用的,由于 XML 是一個開放式標準,因此,對于通過 Web 共享資料而言,這是一個很好的選擇,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 = https://www.cnblogs.com/ayxj/archive/2022/03/13/bFormatHx.Deserialize(streamHx); //反序列化得到的是一個object物件.須做下型別轉換
streamHx.Close();
//SOAP反序列化
FileStream streamSoap = new FileStream(str +"personSoap.xml", FileMode.OpenOrCreate);
SoapFormatter bFormatSoap = new SoapFormatter();
data = https://www.cnblogs.com/ayxj/archive/2022/03/13/bFormatSoap.Deserialize(streamSoap); //反序列化得到的是一個object物件.須做下型別轉換
streamSoap.Close();
//XML反序列化
FileStream streamXml = new FileStream(str +"personXml.xml", FileMode.OpenOrCreate);
XmlSerializer xmlserilize = new XmlSerializer(typeof(Person));
data = https://www.cnblogs.com/ayxj/archive/2022/03/13/xmlserilize.Deserialize(streamXml); //反序列化得到的是一個object物件.須做下型別轉換
streamXml.Close();
}
}
}
人生,挫折與順利同在,給予和索取平衡...
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/442744.html
標籤:.NET技术
