将一个对象序列化成二进制、XML文件,以及反序列化对象。
一、将要被序列化的对象(Object)
using System; namespace SerializationLibrary { /// <summary> /// MyObject实体类,用来作序列化测试 /// </summary> [Serializable] public class MyObject { private int age; /// <summary> /// 年龄 /// </summary> public int Age { get { return age; } set { age = value; } } private string gender; /// <summary> /// 性别 /// </summary> public string Gender { get { return gender; } set { gender = value; } } private string name; /// <summary> /// 姓名 /// </summary> public string Name { get { return name; } set { name = value; } } private string address; /// <summary> /// 地址 /// </summary> public string Address { get { return address; } set { address = value; } } } }
二、处理类:
using SerializationLibrary; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; using System.Xml.Serialization; using System.IO; using System.Windows.Forms; namespace Serialization { /// <summary> /// 可操作序列化的类 /// </summary> public static class SerializableHandler { /// <summary> /// 将被序列化的对象 /// </summary> static MyObject Obj = new MyObject { Name = "Nicholas Nie", Age = 23, Gender = "Man", Address = "Beijing" }; /// <summary> /// 序列化到二进制 /// </summary> public static void SerializationToBinary() { IFormatter Formatter = new BinaryFormatter(); Stream Stream = new FileStream(@"E:\WindowsProgram\Serialization\MyObj.bin", FileMode.Create, FileAccess.Write, FileShare.None); Formatter.Serialize(Stream, Obj); Stream.Close(); FileInfo FileInfo = new FileInfo(@"E:\WindowsProgram\Serialization\MyObj.bin"); if (FileInfo.Exists) { MessageBox.Show("序列化操作成功!\n" + "对象被序列化到" + FileInfo.FullName, "提示!", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show("序列化操作失败!", "出错!", MessageBoxButtons.OK, MessageBoxIcon.Information); } } /// <summary> /// 根据二进制反序列化 /// </summary> /// <returns></returns> public static MyObject DeserializationByBinary() { FileInfo FileInfo = new FileInfo(@"E:\WindowsProgram\Serialization\MyObj.bin"); if (FileInfo.Exists) { IFormatter Formatter = new BinaryFormatter(); Stream Stream = new FileStream(@"E:\WindowsProgram\Serialization\MyObj.bin", FileMode.Open, FileAccess.Read, FileShare.Read); MyObject RestoreObject = (MyObject)Formatter.Deserialize(Stream); Stream.Close(); MessageBox.Show("反序列化操作成功!\n" + "对象类型:" + RestoreObject.ToString() + " \nName:" + RestoreObject.Name + "\nAge:" + RestoreObject.Age + "\nGender:"+RestoreObject.Gender + "\nAddress:"+RestoreObject.Address, "提示!", MessageBoxButtons.OK, MessageBoxIcon.Information); return RestoreObject; } else { MessageBox.Show("反序列化操作失败!\n" + "不存在可反序列化的文件!\n" + "路径" + FileInfo.FullName, "出错!", MessageBoxButtons.OK, MessageBoxIcon.Information); return null; } } /// <summary> /// 序列化到Xml /// </summary> public static void SerializationToXml() { XmlSerializer XmlSer = new XmlSerializer(typeof(MyObject)); StreamWriter SW = new StreamWriter(@"E:\WindowsProgram\Serialization\MyObj.xml"); XmlSer.Serialize(SW, Obj); SW.Close(); FileInfo Fileinfo = new FileInfo(@"E:\WindowsProgram\Serialization\MyObj.xml"); if (Fileinfo.Exists) { MessageBox.Show("序列化操作成功!\n" + "对象被序列化到" + Fileinfo.FullName, "提示!", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show("序列化操作失败!", "出错!", MessageBoxButtons.OK, MessageBoxIcon.Information); } } /// <summary> /// 根据Xml反序列化 /// </summary> /// <returns></returns> public static MyObject DeserializationByXml() { XmlSerializer XmlSer = null; FileStream FileStream = null; FileInfo FileInfo = new FileInfo(@"E:\WindowsProgram\Serialization\MyObj.xml"); if (FileInfo.Exists) { XmlSer = new XmlSerializer(typeof(MyObject)); FileStream = new FileStream(@"E:\WindowsProgram\Serialization\MyObj.xml", FileMode.Open, FileAccess.Read, FileShare.Read); MyObject RestoreObject = (MyObject)XmlSer.Deserialize(FileStream); FileStream.Close(); MessageBox.Show("反序列化操作成功!\n" + "对象类型:" + RestoreObject.ToString() + " \nName:" + RestoreObject.Name + "\nAge:" + RestoreObject.Age + "\nGender:" + RestoreObject.Gender + "\nAddress:" + RestoreObject.Address, "提示!", MessageBoxButtons.OK, MessageBoxIcon.Information); return RestoreObject; } else { MessageBox.Show("序列化操作失败!", "出错!", MessageBoxButtons.OK, MessageBoxIcon.Information); return null; } } } }