序列化

 

 

序列化: 对象-->流-->保存到文件

一个类要想被序列化,必须加 [Serializable]标识为可序列化
二进制序列化器:
命名空间:
using System.Runtime.Serialization.Formatters.Binary;
类:
BinaryFormatter
------------使用二进制序列化器进行序列化
StudentData data = new StudentData();
data.Code = TextBox1.Text;
data.Name = TextBox2.Text;
data.Sex = TextBox3.Text;
data.Nation = TextBox4.Text;

FileStream fs = null;

try
{
string path = Server.MapPath("data/aaa.txt");
fs = new FileStream(path, FileMode.Create);

//开始使用序列化,将对象序列化到流中去
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs, data);//序列化的方法

}
finally
{
if(fs!=null)
{
fs.Close();
}
}


反序列化:流-->对象
-----使用二进制序列化器,进行反序列化
string path = Server.MapPath("data/aaa.txt");
FileStream fs = null;
try {
fs = new FileStream(path,FileMode.Open);
//从流中反序列化出对象
BinaryFormatter bf = new BinaryFormatter();
StudentData data = (StudentData)bf.Deserialize(fs);

TextBox1.Text = data.Code;
TextBox2.Text = data.Name;
TextBox3.Text = data.Sex;
TextBox4.Text = data.Nation;
}
finally
{
if (fs != null)
{
fs.Close();
}
}


------SOAP序列化

 

转载于:https://www.cnblogs.com/981971554nb/p/4506567.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值