序列化

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

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