序列化与反序列化
Microsoft .NET Framework提供如下两种序列化技术:
(1)二进制序列化:可以保持类型不变,即可以在应用程序的不同调用之间保留对象的状态
(2)XML和SOAP序列化:仅序列化公共属性和字段,不保存类型
一、二进制的序列化与反序列化
序列化可以被定义为将对象的状态存储到存储媒介的过程,在二进制的序列化过程中,对象的公共字段和私有字段以及类的名称(包括包含该类的程序集)都被转换为字节流,然后写入数据流。在以后反序列化该对象时,创建原始对象的精确复本。 首先创建一个序列化的类using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Serialize
{
[Serializable]
public class Student
{
private string stuId;
private string stuName;
//此字段不被序列化
[NonSerialized]private string stuSex;
public string StuId { get { return this.stuId; } set { this.stuId = value; } }
public string StuName { get { return this.stuName; } set { this.stuName = value; } }
}
}
在main函数内对其序列化和反序列化
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Xml.Serialization;
namespace Serialize
{
class Program
{
static void Main(string[] args)
{
//二进制序列化与反序列化
Student stu = new Student();
stu.StuId = "1027412214";
stu.StuName = "张三";
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream("StuInfo.bin", FileMode.Create, FileAccess.Write, FileShare.None);
formatter.Serialize(stream, stu);
stream.Close();
stream = new FileStream("StuInfo.bin", FileMode.Open, FileAccess.Read, FileShare.Read);
Student stuBin = (Student)formatter.Deserialize(stream);
stream.Close();
Console.WriteLine("学号:{0}",stuBin.StuId);
Console.WriteLine("姓名:{0}", stuBin.StuName);
Console.ReadKey();
}
}
}
打开应用程序bin子目录下的Debug子目录,会看到有一个文件StuInfo.bin,该文件保存的就是Student对象的值。
二、XML序列化和反序列化
xml序列化仅将对象的公共字段和属性值序列化为xml流,而不转换方法、索引器、私有字段或只读属性(只读集合除外),xml序列化不包括类型信息,即不能保证序列化后的对象在被反序列化时变为同一类型的对象。 使用xml序列化和反序列化using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Xml.Serialization;
namespace Serialize
{
class Program
{
static void Main(string[] args)
{
//XML序列化与反序列化 对象一定要是public类型
//第一步 创建要序列化的对象
Student stu1 = new Student();
stu1.StuId = "1027005469";
stu1.StuName = "李四";
//第二步 构造XmlSerializer对象
XmlSerializer xmlSer = new XmlSerializer(typeof(Student));
//创建StreamWriter对象完成对文件的写操作
StreamWriter sw = new StreamWriter("StuInfo.xml");
//第三步 调用Serialize方法实现序列化
xmlSer.Serialize(sw, stu1);
sw.Close();
//创建FileStream读取文件
FileStream fs = new FileStream("StuInfo.xml", FileMode.Open);
//调用DeSerialize方法并强制转换返回对象类型
Student stuXml = (Student)xmlSer.Deserialize(fs);
fs.Close();
Console.WriteLine("学号:{0}", stuXml.StuId);
Console.WriteLine("姓名:{0}", stuXml.StuName);
Console.ReadKey();
}
}
}
打开应用程序bin子目录下的Debug子目录,会看到有一个文件StuInfo.xml,该文件保存的就是Student对象的值。另附:转载自:http://blog.csdn.net/sunbingzibo/article/details/1728794
System.Runtime.Serialization.Formatters.Soap 命名空间包含SoapFormatter 类,该类用于以SOAP 格式将对象序列化和反序列化。
生成使用此命名空间中的类型的应用程序时,必须引用System.Runtime.Serialization.Formatters.Soap.dll 程序集。
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using System.Xml;
using System.IO;
using System.Text;
using System.Xml.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;
namespace Serialization
{
[Serializable]
public class Car
{
private string _Price;
private string _Owner;
private string m_filename;
[XmlElement(ElementName =
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using System.Xml;
using System.IO;
using System.Text;
using System.Xml.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;
namespace Serialization
{
[Serializable]
public class Car
{
private string _Price;
private string _Owner;
private string m_filename;
[XmlElement(ElementName =