一:BinaryFomatter VS. SoapFomatter
class
Program
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
static void Main(string[] args)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
//UseBinaryFormatter();
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
UseSoapFormatter();
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
Console.ReadLine();
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//*
* 使用BinaryFormatter和SoapFormatter序列化类时,必须要标记[Serializable]属性。
* 不需要有不带任何参数的构造函数也可以反序列化。
* 并且可以序列化所有的变量和属性,而不仅限于公有的成员。
* 对于不需要序列化的成员,可以使用[NonSerialized]属性进行标记。
*/
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
// 使用BinaryFormatter类
public static void UseBinaryFormatter()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
Console.WriteLine("---use BinaryFormatter---");
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
Employee emp = new Employee("Daniel", 100, "Guangzhou", DateTime.Now);
Console.WriteLine("before serialize {0} ", emp.ToString());
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
BinaryFormatter formatter = new BinaryFormatter();
FileStream tofile = new FileStream(@"C:Employee.bin", FileMode.Create);
formatter.Serialize(tofile, emp);
tofile.Close();
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
FileStream fromfile = new FileStream(@"C:Employee.bin", FileMode.Open);
Employee emp1 = (Employee)formatter.Deserialize(fromfile);
fromfile.Close();
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
Console.WriteLine("after serialzie {0} ", emp1.ToString());
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
// 使用SoapFormatter类
public static void UseSoapFormatter()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
Console.WriteLine("---use SoapFormatter---");
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
Employee emp = new Employee("Daniel", 100, "Guangzhou", DateTime.Now);
Console.WriteLine("before serialize {0} ", emp.ToString());
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
SoapFormatter formatter = new SoapFormatter();
FileStream tofile = new FileStream(@"C:Employee.soap", FileMode.Create);
formatter.Serialize(tofile, emp);
tofile.Close();
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
FileStream fromfile = new FileStream(@"C:Employee.soap", FileMode.Open);
Employee emp1 = (Employee)formatter.Deserialize(fromfile);
fromfile.Close();
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
Console.WriteLine("after serialzie {0} ", emp1.ToString());
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
[Serializable]
public
class
Employee
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
public string Name;
public int Age; // 公有变量,但不初始化,也会被序列化
[NonSerialized]
private int Salary;
protected string Address;
public DateTime EnrollmentDate;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
public Employee(string name, int salary, string address, DateTime enrollmentDate)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
this.Name = name;
this.Salary = salary;
this.Address = address;
this.EnrollmentDate = enrollmentDate;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
public override string ToString()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
return string.Format("Name:{0}, Age:{1}, Salary:{2}, Address:{3}", this.Name, this.Age, this.Salary, this.Address);
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
二 XMLFomatter
class
Program
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
static void Main(string[] args)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
Serialize();
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
Deserialize();
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
Console.ReadLine();
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//*
* 使用XmlSerializer序列化类时,该类不需要必须要标记[Serializable]属性。
* 需要有不带任何参数的构造函数。
* 并且只可以序列化公有的成员,私有成员和保护成员在序列化中会丢失。
*/
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
public static void Serialize()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
Console.WriteLine("---Serialize---");
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
Employee emp = new Employee("Daniel", 100, "Guangzhou", new string[] ...{ "020-82319259", "13570479050", "020-12345678"});
Console.WriteLine(emp.ToString());
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
FileStream stream = new FileStream(@"C:Employee.xml", FileMode.Create);
XmlSerializer serializer = new XmlSerializer(typeof(Employee));
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
serializer.Serialize(stream, emp);
stream.Close();
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
public static void Deserialize()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
Console.WriteLine("---Deserialize---");
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
FileStream stream = new FileStream(@"C:Employee.xml", FileMode.Open);
XmlSerializer serializer = new XmlSerializer(typeof(Employee));
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
Employee emp = (Employee)serializer.Deserialize(stream);
stream.Close();
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
Console.WriteLine(emp.ToString());
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
public
class
Employee
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
[XmlAttribute]
public string Name;
public int Age; // 公有变量,但不初始化,也会被序列化
private int Salary;
protected string Address;
[XmlArray(ElementName="Telephones")]
[XmlArrayItem(ElementName="Telephone")]
public string[] Telephone;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
// 为了序列化,所以必须要有不带任何参数的构造函数
public Employee()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
public Employee(string name, int salary, string address, string[] telephone)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
this.Name = name;
this.Salary = salary;
this.Address = address;
this.Telephone = telephone;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
public override string ToString()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
return string.Format("Name:{0}, Age:{1}, Salary:{2}, Address:{3}", this.Name, this.Age, this.Salary, this.Address);
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
三 自定义Fomatter
class
Program
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
static void Main(string[] args)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
Serialize();
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
Deserialize();
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
Console.ReadLine();
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//*
* 使用自定义序列化类时,必须要标记[Serializable]属性,并且要实现ISerializable接口。
*/
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
public static void Serialize()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
Employee emp = new Employee("Daniel", 100, "Guangzhou");
Console.WriteLine("before serialize {0} ", emp.ToString());
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
IFormatter formatter = new SoapFormatter();
FileStream stream = new FileStream(@"C:Employee.soap", FileMode.Create);
formatter.Serialize(stream, emp);
stream.Close();
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
public static void Deserialize()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
IFormatter formatter = new SoapFormatter();
Stream stream = new FileStream(@"C:Employee.soap", FileMode.Open);
Employee emp = (Employee)formatter.Deserialize(stream);
stream.Close();
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
Console.WriteLine("after serialize {0} ", emp.ToString());
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
[Serializable]
public
class
Employee : ISerializable
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
public string Name;
public int Age; // 公有变量,但不初始化,也会被序列化
private int Salary;
protected string Address;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
public Employee(string name, int salary, string address)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
this.Name = name;
this.Salary = salary;
this.Address = address;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
protected Employee(SerializationInfo info, StreamingContext context)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
this.Name = "FromFile:" + info.GetString("Name")
this.Salary = info.GetInt32("Salary");
this.Address = "FromFile:" + info.GetString("Address");
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
info.AddValue("Name", "ToFile:" + this.Name);
info.AddValue("Salary", this.Salary);
info.AddValue("Address", "ToFile:" + this.Address);
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
public override string ToString()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
return string.Format("Name={0}, Age={1}, Salary={2}, Address={3}", this.Name, this.Age, this.Salary, this.Address);
}
}