序列化又称串行化,是.NET运行时环境用来支持用户定义类型的流化的机制。其目的是以某种存储形成使自定义对象持久化,或者将这种对象从一个地方传输到另一个地方。 .NET框架提供了两种种串行化的方式:1、是使用BinaryFormatter进行串行化;2、使用XmlSerializer进行串行化。第一种方式提供了一个简单的二进制数据流以及某些附加的类型信息,而第二种将数据流格式化为XML存储。 可以使用[Serializable]属性将类标志为可序列化的。如果某个类的元素不想被序列化,1、可以使用[NonSerialized]属性来标志,2、可以使用[XmlIgnore]来标志。 序列化意思指的是把对象的当前状态进行持久化,一个对象的状态在面向对象的程序中是由属性表示的,所以序列化类的时候是从属性读取值以某种格式保存下来,而类的成员函数不会被序列化,.net存在几种默认提供的序列化,二进制序列化,xml和json序列化会序列化所有的实例共有属性。
1.BinaryFormatter 二进制序列化 命名空间:System.Runtime.Serialization.Formatters.Binary; 下面先定义一个可序列化类:
Main函数中:
序列化和反序列化的方法: 反序列化输出结果: 大家好,我是张三,今年0岁 大家好,我是李四,今年0岁 由此看出,未序列化的字段存储的值为空。 2、使用XmlSerializer进行串行化 关于格式化器还有一个问题,假设我们需要XML,有两中方案:要么编写一个实现IFormatter接口的类,采用的方式类似于SoapFormatter类,但是没有你不需要的信息;要么使用库类XmlSerializer,这个类不使用Serializable属性,但是它提供了类似的功能。 如果我们不想使用主流的串行化机制,而想使用XmlSeralizer进行串行化我们需要做一下修改: a.添加System.Xml.Serialization命名空间。 b.Serializable和NoSerialized属性将被忽略,而是使用XmlIgnore属性,它的行为与NoSerialized类似。 c.XmlSeralizer要求类有个默认的构造器,这个条件可能已经满足了。 序列化: XmlSerializer xs = new XmlSerializer(typeof(List<Peoson>)); xs.Serialize(fs, listPers); 反序列化: XmlSerializer xs = new XmlSerializer(typeof(List<Peoson>)); List<Peoson> list = xs.Deserialize(fs) as List<Peoson>; 例子: using System.Collections.Generic; using System.IO; using System.Runtime.Serialization.Formatters.Binary; using System.Xml.Serialization; namespace XML { [Serializable]//定义一个可序列化的类 [XmlRoot("AAA")]//设置作为XML中的根元素名称 public class Person { public Person() { } public Person(string name,int age) { this.name = name; this.age = age; } private string name; //[XmlAttribute("Name")] 设置作为xml中的属性 public string Name { get { return name; } set { name = value; } } private int age; // [XmlElement("Age")]设置作为XML中的元素(默认状态) public int Age { get { return age; } set { age = value; } } // [XmlElement("abc")] public int abc = 1000;//类的public属性字段都可以被序列化 public void SayHi() { HttpContext.Current.Response.Write(string.Format("大家好,我是{0},我今年{1}岁", name, age)); } } }
using System.Collections.Generic; using System.IO; using System.Runtime.Serialization.Formatters.Binary; using System.Xml.Serialization; namespace XML { public partial class Xuliehua : System.Web.UI.Page { List<Person> listpers = new List<Person>(); Person p1 = new Person("chengshubo", 23); Person p2 = new Person("xiaomengjie", 22); protected void Page_Load(object sender, EventArgs e) { // List<Person> listpers = new List<Person>(); listpers.Add(p1); listpers.Add(p2);//把类的实例添加到listpers泛型列表中 //SerializeMethod(listpers);//序列化 // ReserializeMethod();//反序列化 } /// <summary> /// 序列化 泛型列表List<Person> listPers 到1.bin文件中存储 /// </summary> /// <param name="listPers"></param> public void SerializeMethod(List<Person> listPers) {
using (FileStream fs = new FileStream(Server.MapPath("~/Xuliehua/1.bin"), FileMode.Create)) { BinaryFormatter bf = new BinaryFormatter();//以二进制文件序列化和反序列化 bf.Serialize(fs, listPers); Response.Write(Server.MapPath("~/Xuliehua/1.bin") + "序列化文件成功"); } } /// <summary> /// 反序列化1.bin 到泛型列表 List<Person> listPers ,然后遍历泛型列表 /// </summary> public void ReserializeMethod() { using (FileStream fs = new FileStream(Server.MapPath("~/Xuliehua/1.bin"), FileMode.Open)) { BinaryFormatter bf = new BinaryFormatter(); List<Person> listPers = bf.Deserialize(fs) as List<Person>; if (listPers != null) { foreach (Person p in listPers) { p.SayHi(); } //for (int i = 0; i < listPers.Count; i++) //{ // listPers[i].SayHi(); //} } } } //序列化一个泛型列表到xml public void XmlSerializeMethod(List<Person> listPers) { using(FileStream fs=new FileStream(Server.MapPath("~/Xuliehua/xuliehua.xml"),FileMode.Create)) { XmlSerializer xs = new XmlSerializer(typeof(List<Person>)); xs.Serialize(fs, listpers); Response.Write(Server.MapPath("~/Xuliehua/xuliehua.xml") + "序列化文件成功"); } } //反序列化一个xml到泛型列表 public void ReXmlSerializeMethod() { using(FileStream fs=new FileStream(Server.MapPath("~/Xuliehua/xuliehua.xml"),FileMode.Open)) { XmlSerializer xs = new XmlSerializer(typeof(List<Person>)); List<Person> listpers = xs.Deserialize(fs) as List<Person>; if (listpers != null) { for (int i = 0; i < listpers.Count; i++) { listpers[i].SayHi(); } } } } //序列化一个类的实例 public void XmlSerializeMethod(Person p) { using (FileStream fs = new FileStream(Server.MapPath("~/Xuliehua/person.xml"), FileMode.Create)) { XmlSerializer xs = new XmlSerializer(typeof(Person)); xs.Serialize(fs, p); Response.Write(Server.MapPath("~/Xuliehua/person.xml") + "序列化文件成功"); } } protected void Button1_Click(object sender, EventArgs e) { SerializeMethod(listpers);//序列化 } protected void Button2_Click(object sender, EventArgs e) { ReserializeMethod();//反序列化 } protected void Button3_Click(object sender, EventArgs e) { XmlSerializeMethod(listpers);//序列化成xml } protected void Button4_Click(object sender, EventArgs e) { ReXmlSerializeMethod();//反序列化 } protected void Button5_Click(object sender, EventArgs e) { XmlSerializeMethod(p1);//序列化类的实例p1 } } }
|