C#序列化(Serialize)、反序列化(Deserialize)
序列化
序列化又称串行化,是.NET运行时环境用来支持用户定义类型的流化的机制。其目的是以某种存储形成使自定义对象持久化,或者将这种对象从一个地方传输到另一个地方。
.NET框架提供了两种串行化的方式:
1、是使用BinaryFormatter进行串行化;
2、使用SoapFormatter进行串行化;
3、使用XmlSerializer进行串行化。
第一种方式提供了一个简单的二进制数据流以及某些附加的类型信息,
而第二种将数据流格式化为XML存储;
第三种其实和第二种差不多也是XML的格式存储,只不过比第二种的XML格式要简化很多(去掉了SOAP特有的额外信息)。
可以使用[Serializable]属性将类标志为可序列化的。如果某个类的元素不想被序列化,1、2可以使用[NonSerialized]属性来标志,2、可以使用[XmlIgnore]来标志。
1、使用BinaryFormatter进行串行化
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
[Serializable]
public class ClassToSerialize
{
public int id = 100;
public string name = "Name";
[NonSerialized] //不会被序列化
public string Sex = "男";
}
public static void SerializeNow()
{
ClassToSerialize c = new ClassToSerialize();
FileStream fileStream = new FileStream("c:\\temp.dat", FileMode.Create);
BinaryFormatter b = new BinaryFormatter();
b.Serialize(fileStream, c);
fileStream.Close();
}
public static void DeSerializeNow()
{
ClassToSerialize c = new ClassToSerialize();
c.Sex = "kkkk";
FileStream fileStream = new FileStream("c:\\temp.dat", FileMode.Open, FileAccess.Read, FileShare.Read);
BinaryFormatter b = new BinaryFormatter();
c = b.Deserialize(fileStream) as ClassToSerialize;
Console.WriteLine(c.name);
Console.WriteLine(c.Sex);
fileStream.Close();
}
Sex属性因为被标志为[NonSerialized],故其值总是为null。
2、使用SoapFormatter进行串行化
using System.Runtime.Serialization.Formatters.Soap; //引入System.Runtime.Serialization.Formatters.Soap类库
public static void SOAPSerializeNow()
{
ClassToSerialize c = new ClassToSerialize();
FileStream fileStream = new FileStream("c:\\temp.xml", FileMode.Create);
SoapFormatter soap = new SoapFormatter();
soap.Serialize(fileStream,c);
fileStream.Close();
}
public static void SOAPDeSerializeNow()
{
ClassToSerialize c = new ClassToSerialize();
c.Sex = "kkkk";
FileStream fileStream = new FileStream("c:\\temp.xml", FileMode.Open, FileAccess.Read, FileShare.Read);
SoapFormatter soap = new SoapFormatter();
c = soap.Deserialize(fileStream) as ClassToSerialize;
Console.WriteLine(c.name);
Console.WriteLine(c.Sex);
fileStream.Close();
}
结果:
<SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:clr="http://schemas.microsoft.com/soap/encoding/clr/1.0" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<a1:ClassToSerialize id="ref-1" xmlns:a1="http://schemas.microsoft.com/clr/nsassem/ConsoleApplication1/ConsoleApplication1%2C%20Version%3D1.0.0.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull">
<id>100</id>
<name id="ref-3">Name</name>
</a1:ClassToSerialize>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
3、使用XmlSerializer进行串行化
[Serializable]
public class Person
{
public string Name { get; set; }
public string Sex { get; set; }
public int Age { get; set; }
public Course[] Courses { get; set; }
public Person()
{
}
public Person(string name)
{
this.Name = name;
this.Sex = "男";
}
}
[Serializable]
public class Course{
public string Name { get; set; }
[XmlIgnore]
public string Description { get; set; }
public Course()
{
}
public Course(string name,string description)
{
this.Name = name;
this.Description = description;
}
}
public static void XMLSerializeNow()
{
Person c = new Person("cyj");
c.Courses = new Course[2];
c.Courses[0] = new Course("英语","交流工具");
c.Courses[1] = new Course("数学","自然科学");
XmlSerializer xs = new XmlSerializer(typeof(Person));
FileStream fileStream = new FileStream("c:\\cyj.xml", FileMode.Create, FileAccess.Write, FileShare.Read);
xs.Serialize(fileStream,c);
fileStream.Close();
}
public static void XMLDeSerializeNow()
{
XmlSerializer xs = new XmlSerializer(typeof(Person));
FileStream fileStream = new FileStream("c:\\cyj.xml", FileMode.Open, FileAccess.Read, FileShare.Read);
Person p = xs.Deserialize(fileStream) as Person;
Console.WriteLine(p.Name);
Console.WriteLine(p.Age.ToString());
Console.WriteLine(p.Courses[0].Name);
Console.WriteLine(p.Courses[0].Description);
Console.WriteLine(p.Courses[1].Name);
Console.WriteLine(p.Courses[1].Description);
fileStream.Close();
}
这里Course类的Description属性值将始终为null,生成的xml文档中也没有该节点,如下图:
<?xml version="1.0"?>
<Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Name>cyj</Name>
<Sex>男</Sex>
<Age>0</Age>
<Courses>
<Course>
<Name>英语</Name>
</Course>
<Course>
<Name>数学</Name>
</Course>
</Courses>
</Person>
4、自定义序列化
如果你希望让用户对类进行串行化,但是对数据流的组织方式不完全满意,那么可以通过在自定义类中实现接口来自定义串行化行为。这个接口只有一个方法,GetObjectData. 这个方法用于将对类对象进行串行化所需要的数据填进SerializationInfo对象。你使用的格式化器将构造SerializationInfo对象,然后在串行化时调用GetObjectData. 如果类的父类也实现了ISerializable,那么应该调用GetObjectData的父类实现。
如果你实现了ISerializable,那么还必须提供一个具有特定原型的构造器,这个构造器的参数列表必须与GetObjectData相同。这个构造器应该被声明为私有的或受保护的,以防止粗心的开发人员直接使用它。
示例如下:
实现ISerializable的类:
[Serializable]
public class Employee : ISerializable
{
public int EmpId = 100;
public string EmpNam = "aaaa";
[NonSerialized]
public string NoSerialString = "NoSerialString-Test";
public Employee()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public Employee(SerializationInfo info,StreamingContext ctx)
{
EmpId = (int)info.GetValue("EmployeeId", typeof(int));
EmpNam = (String)info.GetValue("EmployeeName", typeof(string));
//NoSerialString = (String)info.GetValue("EmployeeString",typeof(string));
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("EmployeeId", EmpId);
info.AddValue("EmployeeName",EmpNam);
//info.AddValue("EmployeeString", NoSerialString);
}
}
public static void OtherEmployeeClassTest()
{
Employee mp = new Employee();
mp.EmpId = 10;
mp.EmpNam = "abc";
mp.NoSerialString = "Hello";
Stream stream = File.Open("c:\\temp3.dat",FileMode.Create);
BinaryFormatter bf = new BinaryFormatter();
Console.WriteLine("Writing Employee Info;");
bf.Serialize(stream,mp);
stream.Close();
//反序列化
Stream stream1 = File.Open("c:\\temp3.dat", FileMode.Open);
BinaryFormatter bf2 = new BinaryFormatter();
Console.WriteLine("Reading Employee Info;");
Employee mp2 = (Employee)bf2.Deserialize(stream1);
stream1.Close();
Console.WriteLine(mp2.EmpId);
Console.WriteLine(mp2.EmpNam);
Console.WriteLine(mp2.NoSerialString);
}
结束
赶紧学习为了早点面试找到工作,加油!