码迷,mamicode.com
首页 > 其他好文 > 详细

序列化

时间:2015-05-15 19:39:09      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:

 

 

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

一个类要想被序列化,必须加 [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序列化

 

序列化

标签:

原文地址:http://www.cnblogs.com/981971554nb/p/4506567.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!