初识序列化和反序列化,使用BinaryFormatter类、ISerializable接口、XmlSerializer类进行序列化和反序列化

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

序列化是将对象转换成字节流的过程,反序列化是把字节流转换成对象的过程。对象一旦被序列化,就可以把对象状态保存到硬盘的某个位置,甚至还可以通过网络发送给另外一台机器上运行的进程。本篇主要包括:

● 使用BinaryFormatter类进行序列化和反序列化

● 使用ISerializable接口自定义序列化过程

● 使用xmlSerializer类进行序列化和反序列化

□ 使用BinaryFormatter类进行序列化和反序列化

首先把需要序列化的类打上[Serializable]特性,如果某个字段不需要被序列化,就打上[NonSerialized]特性。

[Serializable]

publicclassMeeting

{

publicstring_name;

[NonSerialized]

publicstring_location;

publicMeeting(stringname,stringlocation)

{

this._name = name;

this._location = location;

}

}

对象序列化后需要一个载体文件,以下的Meeting.binary文件用来存储对象的状态。

staticvoidMain(string[] args)

{

Meeting m1 =newMeeting("年终总结","青岛");

Meeting m2;

//先序列化

SerializedWithBinaryFormatter(m1,"Meeting.binary");

m2 = (Meeting) DeserializeWithBinaryFormatter("Meeting.binary");

Console.WriteLine(m2._name);

Console.WriteLine(m2._location ?? "_location字段没有被序列化");

Console.ReadKey();

}

//序列化

staticvoidSerializedWithBinaryFormatter(objectobj,stringfileName)

{

//打开文件写成流

Stream streamOut = File.OpenWrite(fileName);

BinaryFormatter formatter =newBinaryFormatter();

//把对象序列化到流中

formatter.Serialize(streamOut, obj);

//关闭流

streamOut.Close();

}

//反序列化

staticobjectDeserializeWithBinaryFormatter(stringfileName)

{

//打开文件读成流

Stream streamIn = File.OpenRead(fileName);

BinaryFormatter formatter =newBinaryFormatter();

objectobj = formatter.Deserialize(streamIn);

streamIn.Close();

returnobj;

}

Meeting.binary文件在bin/debug文件夹中。

□ 使用ISerializable接口自定义序列化过程

如果想对序列化的过程有更多的控制,应该使用ISerializable接口,通过它的GetObjectData方法可以改变对象的字段值。

[Serializable]

publicclassLocation : ISerializable

{

publicintx;

publicinty;

publicstringname;

publicLocation(intx,inty,stringname)

{

this.x = x;

this.y = y;

this.name = name;

}

PRotectedLocation(SerializationInfo info, StreamingContext context)

{

x = info.GetInt32("i");

y = info.GetInt32("j");

name = info.GetString("k");

}

publicvoidGetObjectData(SerializationInfo info, StreamingContext context)

{

info.AddValue("i", x + 1);

info.AddValue("j", y + 1);

info.AddValue("k", name + "HELLO");

}

}

以上,不仅要实现接口方法GetObjectData,还需要提供对象的重载构造函数,从SerializationInfo实例中获取值。

在客户端:

Location loc1 =newLocation(1,2,"qingdao");

Location loc2;

//序列化

SerializedWithBinaryFormatter(loc1, "Location.binary");

loc2 = (Location) DeserializeWithBinaryFormatter("Location.binary");

Console.WriteLine(loc2.x);

Console.WriteLine(loc2.y);

Console.WriteLine(loc2.name);

Console.ReadKey();

以上,使用BinaryFormatter类进行序列化和反序列化,存储的文件格式是二进制的,例如,打开Meeting.binary文件,我们看到:

有时候,我们希望文件的格式是xml。

□ 使用XmlSerializer类进行序列化和反序列化

XmlSerializer类进行序列化的存储文件是xml格式。用XmlSerializer类进行序列化的类不需要打上[Serializable]特性。

publicclassCar

{

[XmlAttribute(AttributeName = "model")]

publicstringtype;

publicstringcode;

[XmlIgnore]

publicintage;

[XmlElement(ElementName = "mileage")]

publicintmiles;

publicStatus status;

publicenumStatus

{

[XmlEnum("使用中")]

Normal,

[XmlEnum("修复中")]

NotUse,

[XmlEnum("不再使用")]

Deleted

}

}

在客户端:

//打开写进流

Stream streamOut = File.OpenWrite("Car.xml");

System.Xml.Serialization.XmlSerializer x =newXmlSerializer(car1.GetType());

//序列化到流中

x.Serialize(streamOut, car1);

streamOut.Close();

//打开读流

Stream streamIn = File.OpenRead("Car.xml");

//反序列化

Car car2 = (Car) x.Deserialize(streamIn);

Console.WriteLine(car2.type);

Console.WriteLine(car2.code);

Console.WriteLine(car2.miles);

Console.WriteLine(car2.status);

Console.ReadKey();

运行,打开bin/debug中的Car.xml文件如下:

<?xml version="1.0"?>

<Car xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" model="sedan">

<code>001</code>

<mileage>1000</mileage>

<status>使用中</status>

</Car>

○ 类名Car成了xml的根节点

○ 打上[XmlAttribute(AttributeName = "model")]特性的字段变成了根节点的属性,AttributeName为属性别名

○ 枚举项可打上[XmlEnum("使用中")]特性

如果一个类中包含集合属性,比如以下的Department类包含一个类型List<Employee>的集合属性Employees。

publicclassDepartment

{

publicDepartment()

{

Employees =newList<Employee>();

}

publicstringName {get;set; }

[XmlArray("Staff")]

publicList<Employee> Employees {get;set; }

}

publicclassEmployee

{

publicstringName {get;set; }

publicEmployee()

{

}

publicEmployee(stringname)

{

Name = name;

}

}

在客户端:

classProgram

{

staticvoidMain(string[] args)

{

var department =newDepartment();

department.Name = "销售部";

department.Employees.Add(newEmployee("张三"));

department.Employees.Add(newEmployee("李四"));

XmlSerializer serializer =newXmlSerializer(department.GetType());

//打开写进流

Stream streamOut = File.OpenWrite("Department.xml");

serializer.Serialize(streamOut, department);

streamOut.Close();

}

}

查看bin/debug中的Department.xml文件。

<?xml version="1.0"?>

<Department xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<Name>销售部</Name>

<Staff>

<Employee>

<Name>张三</Name>

</Employee>

<Employee>

<Name>李四</Name>

</Employee>

</Staff>

</Department>

总结:

1、使用BinaryFormatter类序列化到二进制文件

2、使用XmlSerializer类序列化到xml文件

3、使用ISerializable接口自定义序列化过程

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航