.NET 中的序列化Serialize和反序列化Deserialize

  • 定义  

  把对象转换为字节序列的过程称为对象的序列化。
    
把字节序列恢复为对象的过程称为对象的反序列化。

  • 用途

  在进程之间进行通信的时候,需要把信息转换为自己流,也许是二进制的,发送方需要把这个对象转换为字节序列,才能在网络上传送;接收方则需要把字节序列再恢复为对象。 

  • 3中序列化的方式
  1. Binary
  2. SOAP
  3. XML
  • 示例简单说明

本例中使用对一个简单的对象进行序列化和反序列化的方式。二进制的序列化,我把它存储为BIN文件。反序列化的时候进行读取文件。同样也是XML序列和饭序列化。SOAP也是大同小异。

  1. 创建工程 为了演示方便,可以看见效果,使用asp.net web application.包括一个界面和一个class.
  2. 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;
}
}
}
 
 

 

  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>
 
 

 

图形简陋,大家见笑了。

  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";
}
}
}
 
 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值