1.什么是序列化
序列化是將對象狀態(tài)轉(zhuǎn)換為可保持或傳輸?shù)母袷降倪^程,在序列化過程中,對象的公共字段和私有字段以及類的名稱(包括包含該類的程序集)都被轉(zhuǎn)換為字節(jié)流,然后寫入數(shù)據(jù)流。與序列化相對的是反序列化,它將流轉(zhuǎn)換為對象。這兩個過程結(jié)合起來,可以輕松地存儲和傳輸數(shù)據(jù)。
2.為什么使用序列化
a. 一個原因是將對象的狀態(tài)保持在存儲媒體中,以便可以在以后重新創(chuàng)建精確的副本。
我們經(jīng)常需要將對象的字段值保存到磁盤中,并在以后檢索此數(shù)據(jù)。盡管不使用序列化也能完成這項工作,但這種方法通常很繁瑣而且容易出錯,并且在需要跟蹤對象的層次結(jié)構(gòu)時,會變得越來越復(fù)雜??梢韵胂笠幌戮帉懓罅繉ο蟮拇笮蜆I(yè)務(wù)應(yīng)用程序的情形,程序員不得不為每一個對象編寫代碼,以便將字段和屬性保存至磁盤以及從磁盤還原這些字段和屬性。序列化提供了輕松實現(xiàn)這個目標(biāo)的快捷方法。
b.另一個原因是通過值將對象從一個應(yīng)用程序域發(fā)送到另一個應(yīng)用程序域中。
例如,序列化可用于在 asp.net 中保存會話狀態(tài)并將對象復(fù)制到 Windows 窗體的剪貼板中。遠(yuǎn)程處理還可以使用序列化通過值將對象從一個應(yīng)用程序域傳遞到另一個應(yīng)用程序域中。
公共語言運行時 (CLR) 管理對象在內(nèi)存中的分布,.NET 框架則通過使用反射提供自動的序列化機制。對象序列化后,類的名稱、程序集以及類實例的所有數(shù)據(jù)成員均被寫入存儲媒體中。對象通常用成員變量來存儲對其他實例的引用。類序列化后,序列化引擎將跟蹤所有已序列化的引用對象,以確保同一對象不被序列化多次。.NET 框架所提供的序列化體系結(jié)構(gòu)可以自動正確處理對象圖表和循環(huán)引用。對對象圖表的唯一要求是,由正在進(jìn)行序列化的對象所引用的所有對象都必須標(biāo)記為 Serializable(請參閱基本序列化)。否則,當(dāng)序列化程序試圖序列化未標(biāo)記的對象時將會出現(xiàn)異常。
當(dāng)反序列化已序列化的類時,將重新創(chuàng)建該類,并自動還原所有數(shù)據(jù)成員的值。
2.如何實現(xiàn)對象的序列化及反序列化
要實現(xiàn)對象的序列化,首先要保證該對象可以序列化。而且,序列化只是將對象的屬性進(jìn)行有效的保存,對于對象的一些方法則無法實現(xiàn)序列化的。
實現(xiàn)一個類可序列化的最簡便的方法就是增加Serializable屬性標(biāo)記類。如:
[Serializable()]
public class MEABlock
{
private int m_ID;
public string Caption;
public MEABlock()
{
///構(gòu)造函數(shù)
}
}
即可實現(xiàn)該類的可序列化。注意序列化的類必須為Public,否則不能夠被序列化。
要將該類的實例序列化為到文件中?.NET FrameWork提供了兩種方法:
a .XML序列化
使用 XmLSerializer 類,可將下列項序列化。
公共類的公共讀/寫屬性和字段
實現(xiàn) ICollection 或 IEnumerable 的類。(注意只有集合會被序列化,而公共屬性卻不會。)
XmlElement 對象。
XmlNode 對象。
DataSet 對象。
要實現(xiàn)上述類的實例的序列化,可參照如下例子:
MEABlock myBlock = new MEABlock();
// Insert code to set properties and fields of the object.
XmlSerializer mySerializer = new XmlSerializer(typeof(MEABlock));
// To write to a file, create a StreamWriter object.
StreamWriter myWriter = new StreamWriter("myFileName.xml");
mySerializer.Serialize(myWriter, MEABlock);
需要注意的是XML序列化只會將public的字段保存,對于私有字段不予于保存。
生成的XML文件格式如下:
<MEABlock>
<Caption>Test</Caption>
</MEABlock>
對于對象的反序列化,則如下:
MEABlock myBlock;
// Constructs an instance of the XmlSerializer with the type
// of object that is being deserialized.
XmlSerializer mySerializer = new XmlSerializer(typeof(MEABlock));
// To read the file, creates a FileStream.
FileStream myFileStream = new FileStream("myFileName.xml", FileMode.Open);
// Calls the Deserialize method and casts to the object type.
myBlock = (MEABlock)mySerializer.Deserialize(myFileStream)
b. 二進(jìn)制序列化
與XML序列化不同的是,二進(jìn)制序列化可以將類的實例中所有字段(包括私有和公有)都進(jìn)行序列化操作。這就更方便、更準(zhǔn)確的還原了對象的副本。
要實現(xiàn)上述類的實例的序列化,可參照如下例子:
MEABlock myBlock = new MEABlock();
// Insert code to set properties and fields of the object.
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream("MyFile.bin",FileMode.Create,FileAccess.Write, FileShare.None);
formatter.Serialize(stream, myBlock);
stream.Close();
對于對象的反序列化,則如下:
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream("MyFile.bin", FileMode.Open,FileAccess.Read, FileShare.Read);
MEABlock myBlock = (MEABlock) formatter.Deserialize(stream);
stream.Close();
4、如何變相實現(xiàn)自定義可視化控件的序列化、反序列化
對于WinForm中自定義控件,由于繼承于System.Windows.Form類,而Form類又是從MarshalByRefObject繼承的,窗體本身無法做到序列化,窗體的實現(xiàn)基于Win32下GUI資源,不能脫離當(dāng)前上下文存在。
當(dāng)然可以采用變通的方法實現(xiàn)控件的序列化。這里采用的是記憶類模型。
定義記憶類(其實就是一個可序列化的實體類)用于記錄控件的有效屬性,需要序列化控件的時候,只需要將該控件的實例Copy到記憶類,演變成序列化保存該記憶類的操作。
反序列化是一個逆過程。將數(shù)據(jù)流反序列化成為該記憶類,再根據(jù)該記憶類的屬性生成控件實例。而對于控件的一些事件、方法則可以繼續(xù)使用。
wwf之所以強調(diào)要把類實例化,就是因為工作流和應(yīng)用程序是在不同的線程中。二者之間需要用類作為傳遞數(shù)據(jù)的載體的話,就需要把該類定義為public序列化為二進(jìn)制傳輸。
1.序列化有:二進(jìn)制序列化,soap,xml,自定義。
Soap序列化和二進(jìn)制的使用是一樣的,只是soap生成的是xml文件可以瀏覽而二進(jìn)制的不能瀏覽。還有soap序列化要自己添加引用
2.XML 序列化不轉(zhuǎn)換方法、索引器、私有字段或只讀屬性(只讀集合除外)。要序列化對象的所有字段和屬性(公共的和私有的),請使用
使用 XmLSerializer 類可將下列項序列化:
公共類的公共讀/寫屬性和字段
實現(xiàn) ICollection 或 IEnumerable 的類
注意:只有集合會被序列化,而公共屬性卻不會。
XmlElement 對象
XmlNode 對象
DataSet 對象
[XmlAttribute(AttributeName="員工姓名")] //屬性
[XmlElement(ElementName="員工姓名")] //元素
廢話少說了還是把code貼出來了!
主要就是Company.cs,department.cs,TestCustomSerialize .cs(自定義序列化類),web頁面
Company.cs(類庫):
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Xml.Serialization;
/// <summary>
/// Company 的摘要說明
/// </summary>
[XmlRoot(Namespace="http://www.cpdyj.com">,ElementName="BitTime")] //ElementName指定根結(jié)點名稱
public class Company
{
public Company()
{
//
// TODO: 在此處添加構(gòu)造函數(shù)邏輯
//
}
private string _companyName;
[XmlElement(ElementName="公司名稱")]//指定元素名稱
public string CompanyName
{
get { return _companyName; }
set { _companyName = value; }
}
private string _companyAddress;
[XmlElement(ElementName = "公司地址")]
public string CompanyAddress
{
get { return _companyAddress; }
set { _companyAddress = value; }
}
private string _companyPostCode="100085";
[XmlElement(ElementName = "郵政編碼")]
public string CompanyPostCode
{
get { return _companyPostCode; }
set { _companyPostCode = value; }
}
private string _companyBoss;
[XmlElement(ElementName = "法人代表")]
///<summmary>
///法人代表
///</summary>
public string CompanyBoss
{
get { return _companyBoss; }
set { _companyBoss = value; }
}
//下面屬性與department類進(jìn)行關(guān)聯(lián)
private Department[] _depart;
[XmlElement(ElementName="部門名稱")]
public Department[] Depart
{
get{return _depart;}
set{_depart = value;}
}
}
Department.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Serialization;
/// <summary>
/// Department 的摘要說明
/// </summary>
public class Department
{
public Department()
{
//
// TODO: 在此處添加構(gòu)造函數(shù)邏輯
//
}
private string _departmentName;
[XmlAttribute(AttributeName="部門")]
//[XmlElement(ElementName = "部門")]
public string DepartmentName
{
get { return _departmentName; }
set { _departmentName = value; }
}
private string workerName;
//[XmlAttribute(AttributeName="員工姓名")]
[XmlElement(ElementName = "員工姓名")]
public string WorkerName
{
get { return workerName; }
set { workerName = value; }
}
private int _age;
// [XmlAttribute(AttributeName="年齡")]
[XmlElement(ElementName = "年齡")]
public int Age
{
get { return _age; }
set { _age = value; }
}
}
TestCustomSerialize.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Runtime.Serialization;
using System.Security.Permissions;
/// <summary>
/// TestCustomSerialize 的摘要說明
/// </summary>
public class TestCustomSerialize : ISerializable
{
public TestCustomSerialize()
{
//
// TODO: 在此處添加構(gòu)造函數(shù)邏輯
//
}
public int n1;
public int n2;
public string str;
public DateTime date;
public TestCustomSerialize(SerializationInfo info, StreamingContext context)
{
n1=info.GetInt32("i");
n2=info.GetInt32("j");
str=info.GetString("k");
date = info.GetDateTime("dateTime").ToLocalTime();
}
#region ISerializable 成員
[SecurityPermissionAttribute(SecurityAction.Demand,SerializationFormatter=true)]
public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("i",n1);
info.AddValue("j",n2);
info.AddValue("k",str);
info.AddValue("dateTime", date);
}
#endregion
}
Web頁面(前臺就是幾個按鈕,別浪費頁面了)
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Runtime.Serialization.Formatters.Soap;
using System.Xml.Serialization;
public partial class 序列化 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
//二進(jìn)制序列化
protected void Button1_Click(object sender, EventArgs e)
{
MyObject obj = new MyObject();
obj.n1 = 1;
obj.n2 = 24;
obj.str = "Some String";
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream(@"D:\Bit\Log4net\formatter.bin", FileMode.Create, FileAccess.Write, FileShare.None);
formatter.Serialize(stream, obj);
stream.Close();
}
//二進(jìn)制反序列化
protected void Button2_Click(object sender, EventArgs e)
{
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream(@"D:\Bit\Log4net\formatter.bin", FileMode.Open, FileAccess.Read, FileShare.Read);
MyObject obj = (MyObject)formatter.Deserialize(stream);
stream.Close();
Response.Write(obj.n1 + " " + obj.n2 + " " + obj.str);
}
//soap序列化
protected void Button5_Click(object sender, EventArgs e)
{
//soap序列化與二進(jìn)制一樣只是生成的是xml文件而不是二進(jìn)制代碼
//使用soap序列化時必須添加System.Runtime.Serialization.Formatters.Soap的引用,然后引用該命名空間
MyObject obj = new MyObject();
obj.n1 = 1;
obj.n2 = 24;
obj.str = "Some String";
IFormatter formatter = new SoapFormatter();
Stream stream = new FileStream(@"D:\Bit\Log4net\formatter.bin", FileMode.Create, FileAccess.Write, FileShare.None);
formatter.Serialize(stream, obj);
stream.Close();
}
//soap反序列化
protected void Button6_Click(object sender, EventArgs e)
{
IFormatter formatter = new SoapFormatter();
Stream stream = new FileStream(@"D:\Bit\Log4net\formatter.bin", FileMode.Open, FileAccess.Read, FileShare.Read);
MyObject obj = (MyObject)formatter.Deserialize(stream);
stream.Close();
Response.Write(obj.n1 + " " + obj.n2 + " " + obj.str);
}
//xml序列化
protected void Button3_Click(object sender, EventArgs e)
{
Company company = new Company();
company.CompanyName = "北京";
company.CompanyBoss = "海淀";
company.CompanyAddress = "北京海淀區(qū)蘇州街北段";
XmlSerializer xml = new XmlSerializer(typeof(Company));
Stream stream = new FileStream(Server.MapPath("company.xml"), FileMode.Create, FileAccess.Write, FileShare.None);
xml.Serialize(stream, company);
stream.Close();
}
//xml反序列化
protected void Button4_Click(object sender, EventArgs e)
{
XmlSerializer xml = new XmlSerializer(typeof(Company));
Stream stream = new FileStream(Server.MapPath("company.xml"), FileMode.Open, FileAccess.Read, FileShare.Read);
Company company = (Company)xml.Deserialize(stream);
Response.Write(company.CompanyAddress + "," + company.CompanyBoss + "," + company.CompanyName + "," + company.CompanyPostCode);
}
protected void Button7_Click(object sender, EventArgs e)
{
Company company = new Company();
company.CompanyName = "北京";
company.CompanyBoss = "海淀";
company.CompanyAddress = "北京海淀區(qū)蘇州街北段";
//實例化Department類
Department d1 = new Department();
d1.Age = 30;
d1.WorkerName = "Hello";
d1.DepartmentName = "技術(shù)部";
Department d2 = new Department();
d2.Age = 30;
d2.WorkerName = "Hello";
d2.DepartmentName = "技術(shù)部";
Department d3 = new Department();
d3.Age = 30;
d3.WorkerName = "Hello";
d3.DepartmentName = "技術(shù)部";
Department d4 = new Department();
d4.Age = 30;
d4.WorkerName = "Hello";
d4.DepartmentName = "技術(shù)部";
Department d5 = new Department();
d5.Age = 30;
d5.WorkerName = "Hello";
d5.DepartmentName = "技術(shù)部";
Department d6 = new Department();
d6.Age = 30;
d6.WorkerName = "Hello";
d6.DepartmentName = "技術(shù)部";
Department d7 = new Department();
d7.Age = 30;
d7.WorkerName = "Hello";
d7.DepartmentName = "技術(shù)部";
Department[] d ={ d1, d2, d3, d4, d5, d6, d7 };
company.Depart = d;
XmlSerializer xml = new XmlSerializer(typeof(Company));
FileStream stream = new FileStream(Server.MapPath("company.xml"), FileMode.Create);
xml.Serialize(stream, company);
stream.Close();
}
//xml反序列化(有兩個類關(guān)聯(lián))
protected void Button8_Click(object sender, EventArgs e)
{
}
//自定義序列化
protected void Button9_Click(object sender, EventArgs e)
{
//自定義序列化主要是類的寫法不一樣,調(diào)用的方法是一樣的
XmlSerializer xml = new XmlSerializer(typeof(TestCustomSerialize));
string file = Server.MapPath("custom.xml");
if (!File.Exists(file))
{
File.Create(file);
}
else
{
Stream stream = new FileStream(file, FileMode.Create,FileAccess.Write,FileShare.ReadWrite);
TestCustomSerialize custom = new TestCustomSerialize();
custom.str = "歡迎你光臨";
custom.n1 = 2000;
custom.n2 = 1000;
custom.date = DateTime.Now;
xml.Serialize(stream, custom);
stream.Close();
}
}
//自定義反序列化
protected void Button10_Click(object sender, EventArgs e)
{
string file = Server.MapPath("custom.xml");
Stream stream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.None);
XmlSerializer xml = new XmlSerializer(typeof(TestCustomSerialize));
TestCustomSerialize test = (TestCustomSerialize)xml.Deserialize(stream);
Response.Write("反序列化結(jié)果:" + test.n1 + "," + test.n2 + "," + test.str + ",當(dāng)前日期是:" + test.date);