.NET 中的序列化Serialize和反序列化Deserialize
- 定义
把对象转换为字节序列的过程称为对象的序列化。
把字节序列恢复为对象的过程称为对象的反序列化。
- 用途
在进程之间进行通信的时候,需要把信息转换为自己流,也许是二进制的,发送方需要把这个对象转换为字节序列,才能在网络上传送;接收方则需要把字节序列再恢复为对象。
- 3中序列化的方式
- Binary
- SOAP
- XML
- 示例简单说明
本例中使用对一个简单的对象进行序列化和反序列化的方式。二进制的序列化,我把它存储为BIN文件。反序列化的时候进行读取文件。同样也是XML序列和饭序列化。SOAP也是大同小异。
- 创建工程 为了演示方便,可以看见效果,使用asp.net web application.包括一个界面和一个class.
- Class 建立简单对象
Code
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace Serializetion
{
[Serializable]
public class MyObject
{
private string _Id;
private string _Name;
public string Id
{
get { return this._Id; }
set { this._Id = value; }
}
public string Name
{
get { return this._Name; }
set { this._Name = value; }
}
public MyObject(string i, string n)
{
this.Id = i;
this.Name = n;
}
}
}
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace Serializetion
{
[Serializable]
public class MyObject
{
private string _Id;
private string _Name;
public string Id
{
get { return this._Id; }
set { this._Id = value; }
}
public string Name
{
get { return this._Name; }
set { this._Name = value; }
}
public MyObject(string i, string n)
{
this.Id = i;
this.Name = n;
}
}
}
3.简单界面
Code
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="ID_LB" runat="server" Text="ID"></asp:Label>
<asp:TextBox ID="idTB" runat="server" Text=""></asp:TextBox>
<asp:Label ID="message" runat="server" Text=""></asp:Label>
</div>
<div>
<asp:Label ID="name_LB" runat="server" Text="name"></asp:Label>
<asp:TextBox ID="nameTB" runat="server" Text=""></asp:TextBox>
</div>
<div>
<asp:Button ID="btnSeBinary" runat="server" Text="toBinary" OnClick="btnSeBinary_Click" />
<asp:Button ID="btnDeserialize" runat="server" Text="Deserialize" OnClick="btnDeserialize_Click" />
</div>
<div>
<asp:Button ID="btnToXML" runat="server" Text="toXML" OnClick="btnToXML_Click" />
<asp:Button ID="deseXML" runat="server" Text="deseXML" OnClick="deseXML_Click" />
</div>
</form>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="ID_LB" runat="server" Text="ID"></asp:Label>
<asp:TextBox ID="idTB" runat="server" Text=""></asp:TextBox>
<asp:Label ID="message" runat="server" Text=""></asp:Label>
</div>
<div>
<asp:Label ID="name_LB" runat="server" Text="name"></asp:Label>
<asp:TextBox ID="nameTB" runat="server" Text=""></asp:TextBox>
</div>
<div>
<asp:Button ID="btnSeBinary" runat="server" Text="toBinary" OnClick="btnSeBinary_Click" />
<asp:Button ID="btnDeserialize" runat="server" Text="Deserialize" OnClick="btnDeserialize_Click" />
</div>
<div>
<asp:Button ID="btnToXML" runat="server" Text="toXML" OnClick="btnToXML_Click" />
<asp:Button ID="deseXML" runat="server" Text="deseXML" OnClick="deseXML_Click" />
</div>
</form>
</body>
</html>
图形简陋,大家见笑了。
4.后台以二进制和XML序列化为例,进行读取和写操作。
Code
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.IO;
using System.Drawing;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;
namespace Serializetion
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Initialize();
}
}
private void Initialize()
{
this.idTB.Text = "";
this.nameTB.Text = "";
this.message.Text = "";
}
protected void btnSeBinary_Click(object sender, EventArgs e)
{
MyObject myobject = new MyObject(this.idTB.Text, this.nameTB.Text);
FileStream fileStream = new FileStream(@"d:\1\seriTobinary.bin", FileMode.Create);
BinaryFormatter binary = new BinaryFormatter();
binary.Serialize(fileStream, myobject);
fileStream.Close();
this.message.Text = "successfull";
}
protected void btnDeserialize_Click(object sender, EventArgs e)
{
System.Runtime.Serialization.IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream(@"d:\1\seriTobinary.bin", FileMode.Open, FileAccess.Read, FileShare.Read);
MyObject myobject = (MyObject)formatter.Deserialize(stream);
stream.Close();
this.message.Text = "success";
this.idTB.Text = myobject.Id;
this.nameTB.Text = myobject.Name;
}
protected void btnToXML_Click(object sender, EventArgs e)
{
MyObject myobject = new MyObject(this.idTB.Text, this.nameTB.Text);
FileStream fileStream = new FileStream(@"d:\1\seriToXML.XML", FileMode.Create);
SoapFormatter soapformatter = new SoapFormatter();
soapformatter.Serialize(fileStream, myobject);
this.message.Text = "success";
fileStream.Close();
}
protected void deseXML_Click(object sender, EventArgs e)
{
System.Runtime.Serialization.IFormatter formatter = new SoapFormatter();
Stream stream = new FileStream(@"d:\1\seriToXML.XML", FileMode.Open, FileAccess.Read, FileShare.Read);
MyObject myobject = (MyObject)formatter.Deserialize(stream);
stream.Close();
this.idTB.Text = myobject.Id;
this.nameTB.Text = myobject.Name;
this.message.Text = "successful";
}
}
}
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.IO;
using System.Drawing;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;
namespace Serializetion
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Initialize();
}
}
private void Initialize()
{
this.idTB.Text = "";
this.nameTB.Text = "";
this.message.Text = "";
}
protected void btnSeBinary_Click(object sender, EventArgs e)
{
MyObject myobject = new MyObject(this.idTB.Text, this.nameTB.Text);
FileStream fileStream = new FileStream(@"d:\1\seriTobinary.bin", FileMode.Create);
BinaryFormatter binary = new BinaryFormatter();
binary.Serialize(fileStream, myobject);
fileStream.Close();
this.message.Text = "successfull";
}
protected void btnDeserialize_Click(object sender, EventArgs e)
{
System.Runtime.Serialization.IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream(@"d:\1\seriTobinary.bin", FileMode.Open, FileAccess.Read, FileShare.Read);
MyObject myobject = (MyObject)formatter.Deserialize(stream);
stream.Close();
this.message.Text = "success";
this.idTB.Text = myobject.Id;
this.nameTB.Text = myobject.Name;
}
protected void btnToXML_Click(object sender, EventArgs e)
{
MyObject myobject = new MyObject(this.idTB.Text, this.nameTB.Text);
FileStream fileStream = new FileStream(@"d:\1\seriToXML.XML", FileMode.Create);
SoapFormatter soapformatter = new SoapFormatter();
soapformatter.Serialize(fileStream, myobject);
this.message.Text = "success";
fileStream.Close();
}
protected void deseXML_Click(object sender, EventArgs e)
{
System.Runtime.Serialization.IFormatter formatter = new SoapFormatter();
Stream stream = new FileStream(@"d:\1\seriToXML.XML", FileMode.Open, FileAccess.Read, FileShare.Read);
MyObject myobject = (MyObject)formatter.Deserialize(stream);
stream.Close();
this.idTB.Text = myobject.Id;
this.nameTB.Text = myobject.Name;
this.message.Text = "successful";
}
}
}
作者:Alexliu(alex dotNet Learning)
出处:http://alexliu.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,转载请注明。并且保留文章链接。否则保留追究法律责任的权利。