C# 序列化、反序列化 Serialize
二进制序列化:对象序列化之后是二进制形式的,通过BinaryFormatter类来实现的,这个类位于System.Runtime.Serialization.Formatters.Binary命名空间下。
SOAP序列化:对象序列化之后的结果符合SOAP协议,也就是可以通过SOAP 协议传输,通过System.Runtime.Serialization.Formatters.Soap命名空间下的SoapFormatter类来实现的。
XML序列化:对象序列化之后的结果是XML形式的,通过XmlSerializer 类来实现的,这个类位于System.Xml.Serialization命名空间下。XML序列化不能序列化私有数据。
二进制序列化
我们先创建一个Student类,并为它添加上Serializable特性,如果没有添加是不能进行序列化操作的。
[Serializable]
public class Student
{
public string Name { get; set; }
public int Age { get; set; }
public string Email { get; set; }
public Student(string name, int age, string email)
{
this.Name = name;
this.Age = age;
this.Email = email;
}
}
再创建一个List对象,用于存放Student对象
List<Student> students = new List<Student>();
students.Add(new Student("Oliver", 18, "123@qq.com"));
students.Add(new Student("小白", 19, "123@sina.com"));
students.Add(new Student("老K", 28, "123@163.com"));
students.Add(new Student("土豆", 16, "232@sina.com"));
students.Add(new Student("番茄", 33, "fanqie@qq.com"));
students.Add(new Student("小红", 22, "24424@163.com"));
students.Add(new Student("黎明", 18, "2232@126.com"));
students.Add(new Student("黄昏", 21, "huanghun@qq.com"));
使用BinaryFormatter 序列化反序列化
string directoryPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Serialize");
if (!System.IO.Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}
{
string fileName = Path.Combine(directoryPath, "BinaryFormatter.txt");
//序列化
using (Stream stream = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite))
{
BinaryFormatter binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(stream, students);
}
//反序列化
using (Stream stream = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite))
{
BinaryFormatter binaryFormatter = new BinaryFormatter();
stream.Position = 0;//重置流位置
var newList = (List<Student>)binaryFormatter.Deserialize(stream);
}
}
SOAP序列化
string fileName = Path.Combine(directoryPath, "SoapFormatter.txt");
using (Stream stream = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite))
{
SoapFormatter soapFormatter = new SoapFormatter();
soapFormatter.Serialize(stream, students.ToArray());//注意,这儿不能传递泛型对象,否则会出错
//如确实需要传递泛型对象,请使用下面方式传递StudentList:
//[serializble]
//public class StudentList : List <Student>
}
using (Stream stream = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite))
{
SoapFormatter soapFormatter = new SoapFormatter();
var newList = ((Student[])soapFormatter.Deserialize(stream)).ToList();
}
XML序列化
注意:XmlFormatter序列化要求类必须有无参数构造函数,而且各属性必须既能读又能写,即必须同时定义getter和setter,若只定义getter,则反序列化后的得到的各个属性的值都为null。
//序列化
string fileName = Path.Combine(directoryPath, "XmlSerializer.txt");
using (Stream stream = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite))
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<Student>));//创建XML序列化器,需要指定对象的类型
xmlSerializer.Serialize(stream, students);
}
//反序列化
using (Stream stream = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite))
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<Student>));
stream.Position = 0;//重置流位置
var newList = (List<Student>)xmlSerializer.Deserialize(stream);
}
JSON序列化
Json序列化反序列化推荐使用Newtonsoft.Json.dll,它比原生态的Json序列化效率还高,而且使用还很方便。
//使用Newtonsoft序列化
string jsonStr = Newtonsoft.Json.JsonConvert.SerializeObject(students);
//反序列化
var newList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Student>>(jsonStr);