概念:
序列化就是将对象转换为可保持或传输的过程,与序列化相反的过程就是反序列化。
作用:
通过序列化可以很轻松的存储和传输对象。
三种序列化技术:
.NET提供了三种序列化技术,我只知道这三种技术。分别是:Xml序列化、二进制序列化、Soap序列化(需要在项目内添加引用System.Runtime.Serialization.Formatters.Soap,然后在类中using这个引用。)
Xml序列化是将对象的公共属性和字段转换为序列格式并保存在Xml文件中。Xml反序列化则是将Xml中的数据重新生成对象。
二进制序列化指使用二进制编码生成精简的序列化,用于存储或基于套接字的网络流。二进制序列化保持类型保真度,这对于应用程序的不同调用之间保留对象的状态很有用。
Soap序列化是将对象序列化为符合Soap规范的Xml流。
序列化的步骤:
1、创建对象,并设置公共属性和字段
2、确定使用xml、二进制、Soap这三种方式中的哪种方式序列化对象。具体的步骤如下面的示例。
示例:
1、新建解决方案Serializer
2、新建类库Jack.Gao.Domain,并在该类库下添加Person类,Person类的代码如下:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Runtime.Serialization.Formatters; 6 7 namespace Jack.Gao.Domain 8 { 9 [Serializable] 10 public class Person 11 { 12 //如果不想实例化某一个字段,可以使用特性NonSerialized标记 13 private int _id; 14 public int Id 15 { 16 get { return _id; } 17 set { _id = value; } 18 } 19 20 private string _name; 21 public string Name 22 { 23 get { return _name; } 24 set { _name = value; } 25 } 26 27 private string _sex; 28 public string Sex 29 { 30 get { return _sex; } 31 set { _sex = value; } 32 } 33 } 34 }
3、新建类库Jack.Gao.PersonFactory,在该库下新建Factory类,用于生成Person。代码如下:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 using Jack.Gao.Domain; 7 8 namespace Jack.Gao.PersonFactory 9 { 10 public class Factory 11 { 12 #region Create Person 13 public static Person CreatePerson() 14 { 15 return new Person(); 16 } 17 #endregion 18 } 19 }
添加枚举类型SerializerType,代码如下:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace Jack.Gao.PersonFactory 7 { 8 public enum SerializerType 9 { 10 Xml, 11 Binary, 12 Soap 13 } 14 }
再新建一个类SerializerTypeFactory,添加如下代码:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.IO; 6 using System.Runtime.Serialization.Formatters.Binary; 7 using System.Xml.Serialization; 8 using System.Diagnostics; 9 using System.Runtime.Serialization.Formatters.Soap; 10 using Jack.Gao.Domain; 11 12 namespace Jack.Gao.PersonFactory 13 { 14 public static class SerializerTypeFactory 15 { 16 public static object CreateSerializerType(SerializerType serializerType) 17 { 18 object serializer=null; 19 20 switch (serializerType) 21 { 22 case SerializerType.Xml: 23 serializer = new XmlSerializer(typeof(Person)); 24 break; 25 case SerializerType.Binary: 26 serializer = new BinaryFormatter(); 27 break; 28 case SerializerType.Soap: 29 serializer = new SoapFormatter(); 30 break; 31 default: 32 throw new Exception("Type is not correctly!"); 33 } 34 35 return serializer; 36 } 37 } 38 }
4、新建类库Jack.Gao.CreateSerializerType,新建ICreateSerializer接口和CreateSerializer类。
ICreateSerializer的代码如下:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace Jack.Gao.CreateSerializerType 7 { 8 public interface ICreateSerializer 9 { 10 void XmlSerializePerson(); 11 void DeXmlSerializePerson(); 12 void UseBinarySerializePerson(); 13 void UseBinaryDeSerializePerson(); 14 void UseSoapFormatterSerializePerson(); 15 void UseSoapFormatterDeSerializePerson(); 16 } 17 }
CreateSerializer的代码如下:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.IO; 6 using System.Runtime.Serialization.Formatters.Binary; 7 using System.Xml.Serialization; 8 using System.Diagnostics; 9 using System.Runtime.Serialization.Formatters.Soap; 10 11 using Jack.Gao.Domain; 12 using Jack.Gao.PersonFactory; 13 14 15 namespace Jack.Gao.CreateSerializerType 16 { 17 public class CreateSerializer:ICreateSerializer 18 { 19 private static Person _person; 20 21 #region 使用XmlSerializer序列化Person 22 /// <summary> 23 /// 序列化Person,使用XmlSerializer的Serialize进行序列化。 24 /// </summary> 25 public void XmlSerializePerson() 26 { 27 using (FileStream fileStream = new FileStream("Person.xml", FileMode.Create)) 28 { 29 _person = Factory.CreatePerson(); 30 31 _person.Id = 1; 32 _person.Name = "Jack Gao"; 33 _person.Sex = "Man"; 34 35 XmlSerializer xmlSerializer = (XmlSerializer)SerializerTypeFactory.CreateSerializerType(SerializerType.Xml); 36 xmlSerializer.Serialize(fileStream, _person); 37 } 38 } 39 #endregion 40 41 #region 使用XmlSerializer反序列化Person 42 /// <summary> 43 /// 反序列化Person,使用XmlSerializer的Deserialize进行反序列化。 44 /// </summary> 45 public void DeXmlSerializePerson() 46 { 47 _person = Factory.CreatePerson(); 48 49 using (FileStream fileStream = new FileStream("Person.xml", FileMode.Open)) 50 { 51 XmlSerializer xmlSerializer = (XmlSerializer)SerializerTypeFactory.CreateSerializerType(SerializerType.Xml); 52 _person = (Person)xmlSerializer.Deserialize(fileStream); 53 } 54 55 Console.WriteLine(_person.Id); 56 Console.WriteLine(_person.Name); 57 Console.WriteLine(_person.Sex); 58 } 59 #endregion 60 61 #region 使用BinaryFormatter序列化Person 62 public void UseBinarySerializePerson() 63 { 64 _person = Factory.CreatePerson(); 65 66 _person.Id = 2; 67 _person.Name = "Jim"; 68 _person.Sex = "Women"; 69 70 using (FileStream fileStream = new FileStream("Person.xml", FileMode.Create)) 71 { 72 BinaryFormatter binaryFormatter = (BinaryFormatter)SerializerTypeFactory.CreateSerializerType(SerializerType.Binary); 73 binaryFormatter.Serialize(fileStream, _person); 74 } 75 } 76 #endregion 77 78 #region 使用BinaryFormatter反序列化Person 79 public void UseBinaryDeSerializePerson() 80 { 81 Person person = Factory.CreatePerson(); 82 83 using (FileStream fileStream = new FileStream("Person.xml", FileMode.Open)) 84 { 85 BinaryFormatter binaryFormatter = (BinaryFormatter)SerializerTypeFactory.CreateSerializerType(SerializerType.Binary); 86 person = (Person)binaryFormatter.Deserialize(fileStream); 87 } 88 89 Console.WriteLine(person.Id); 90 Console.WriteLine(person.Name); 91 Console.WriteLine(person.Sex); 92 } 93 #endregion 94 95 #region 使用SoapFormatter序列化Person 96 public void UseSoapFormatterSerializePerson() 97 { 98 _person = Factory.CreatePerson(); 99 100 _person.Id = 3; 101 _person.Name = "Green"; 102 _person.Sex = "Man"; 103 104 FileStream fileStream = new FileStream("Person.xml", FileMode.Create); 105 SoapFormatter soapFormatter = (SoapFormatter)SerializerTypeFactory.CreateSerializerType(SerializerType.Soap); 106 soapFormatter.Serialize(fileStream, _person); 107 } 108 #endregion 109 110 #region 使用SoapFormatter反序列化Person 111 public void UseSoapFormatterDeSerializePerson() 112 { 113 _person = Factory.CreatePerson(); 114 115 FileStream fileStream = new FileStream("Person.xml", FileMode.Open); 116 SoapFormatter soapFormatter = (SoapFormatter)SerializerTypeFactory.CreateSerializerType(SerializerType.Soap); 117 _person = (Person)soapFormatter.Deserialize(fileStream); 118 119 Console.WriteLine(_person.Id); 120 Console.WriteLine(_person.Name); 121 Console.WriteLine(_person.Sex); 122 } 123 #endregion 124 } 125 }
5、新建控制台输出程序Jack.Gao.AppSerializer,在Program.cs添加如下代码:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.IO; 6 using System.Runtime.Serialization.Formatters.Binary; 7 using System.Xml.Serialization; 8 using System.Diagnostics; 9 using System.Runtime.Serialization.Formatters.Soap; 10 11 using Jack.Gao.CreateSerializerType; 12 using Jack.Gao.Domain; 13 using Jack.Gao.PersonFactory; 14 15 namespace Jack.Gao.AppSerializer 16 { 17 class Program 18 { 19 static void Main(string[] args) 20 { 21 CreateSerializer createSerializer = new CreateSerializer(); 22 //createSerializer.XmlSerializePerson(); 23 //createSerializer.DeXmlSerializePerson(); 24 25 //createSerializer.UseBinarySerializePerson(); 26 //createSerializer.UseBinaryDeSerializePerson(); 27 28 //createSerializer.UseSoapFormatterSerializePerson(); 29 createSerializer.UseSoapFormatterDeSerializePerson(); 30 ShowIsSuccess(); 31 32 Console.ReadKey(); 33 } 34 35 private static void ShowIsSuccess() 36 { 37 if (IsExitFile()) 38 { 39 Console.WriteLine("Create File sucessfull."); 40 } 41 else 42 { 43 Console.WriteLine("Create File error."); 44 } 45 } 46 47 private static bool IsExitFile() 48 { 49 bool exitXmlFile = false; 50 51 string currentApplicationPath = Process.GetCurrentProcess().MainModule.FileName; 52 53 if (!string.IsNullOrEmpty(currentApplicationPath)) 54 { 55 exitXmlFile = true; 56 } 57 else 58 { 59 exitXmlFile = false; 60 } 61 62 return exitXmlFile; 63 } 64 } 65 }
运行该程序,可以看到在输出目录下多了一个Person.xml的文件,可以查看里面的内容,以下内容使用soap序列化后生成的,你也可以查看其它的两种方式生成的内容。