http://blog.csdn.net/zhoufoxcn/archive/2009/03/11/3978874.aspx

序列化是将对象状态转换为可保持或传输的格式的过程。与序列化相对的是反序列化,它将流转换为对象。这两个过程结合起来,可以轻松地存储和传输数据。例如,可以序列化一个对象,然后使用 HTTP 通过 Internet 在客户端和服务器之间传输该对象。反之,反序列化根据流重新构造对象。此外还可以将对象序列化后保存到本地,再次运行的时候可以从本地文件中“恢复”对象到序列化之前的状态。
在.net中有提供了几种序列化的方式:
二进制序列化
XML序列化
SOAP序列化

二进制序列化
所谓二进制序列化,指的是对象序列化之后是二进制形式的。二进制序列化是通过BinaryFormatter类来实现的,这个类位于System.Runtime.Serialization.Formatters.Binary命名空间下。

XML序列化
所谓XML序列化,是指对象序列化之后的结果是XML形式的。保存XML序列化是通过XmlSerializer 类来实现的, 这个类位于System.Xml.Serialization命名空间下。

SOAP序列化
所谓SOAP序列化是指对象序列化之后的结果符合SOAP协议,也就是可以通过SOAP协议传输(不知道SOAP协议?百度一下吧)。SOAP序列化是通过SoapFormatter类来实现的,这个类位于System.Runtime.Serialization.Formatters.Soap命名空间下,并且需要注意需要手动添加对这个命名空间的引用,如下图所示:
 

下面编写一个类用于序列化和反序列化,这个类的代码如下:

using System;  
using System.Collections.Generic;  
using System.Text;  
 
namespace MySerializeDemo  
{  
    [Serializable]  
    /// <summary>  
    /// 要序列化的对象  
    /// 作者:周公  
    /// 编写时间:2009-03-10  
    /// </summary>  
    public class MyObject  
    {  
        //[NonSerialized]  
        private string name;  
        private DateTime birthday;  
        private string homePlace;  
        /// <summary>  
        /// 出生地  
        /// </summary>  
        public string HomePlace  
        {  
            get { return homePlace; }  
            set { homePlace = value; }  
        }  
   
        /// <summary>  
        /// 生日  
        /// </summary>  
        public DateTime Birthday  
        {  
            get { return birthday; }  
            set { birthday = value; }  
        }  
   
        /// <summary>  
        /// 姓名  
        /// </summary>  
        public string Name  
        {  
            get { return name; }  
            set { name = value; }  
        }  
   
        /// <summary>  
        /// 年龄  
        /// </summary>  
        public int Age  
        {  
            get { return DateTime.Now.Year - birthday.Year; }  
        }  
        /// <summary>  
        /// override了ToString()方法  
        /// </summary>  
        /// <returns></returns>  
        public override string ToString()  
        {  
            return string.Format("姓名:{0},生日:{1},出生地:{2},年龄:{3}",name,birthday,homePlace,Age);  
        }  
   
    }  

using System;
using System.Collections.Generic;
using System.Text;

namespace MySerializeDemo
{
    [Serializable]
    /// <summary>
    /// 要序列化的对象
    /// 作者:周公
    /// 编写时间:2009-03-10
    /// </summary>
    public class MyObject
    {
        //[NonSerialized]
        private string name;
        private DateTime birthday;
        private string homePlace;
        /// <summary>
        /// 出生地
        /// </summary>
        public string HomePlace
        {
            get { return homePlace; }
            set { homePlace = value; }
        }
 
        /// <summary>
        /// 生日
        /// </summary>
        public DateTime Birthday
        {
            get { return birthday; }
            set { birthday = value; }
        }
 
        /// <summary>
        /// 姓名
        /// </summary>
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
 
        /// <summary>
        /// 年龄
        /// </summary>
        public int Age
        {
            get { return DateTime.Now.Year - birthday.Year; }
        }
        /// <summary>
        /// override了ToString()方法
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return string.Format("姓名:{0},生日:{1},出生地:{2},年龄:{3}",name,birthday,homePlace,Age);
        }
 
    }
}
下面是分别用上面的三个类进行序列化和反序列化的代码:

view plaincopy to clipboardprint?
01.using System;  
02.using System.Collections.Generic;  
03.using System.Text;  
04.using System.IO;  
05.using System.Runtime.Serialization.Formatters;  
06.using System.Runtime.Serialization.Formatters.Binary;  
07.using System.Runtime.Serialization.Formatters.Soap;  
08.using System.Xml.Serialization;  
09. 
10.namespace MySerializeDemo  
11.{  
12.    class Program  
13.    {  
14.        static void Main(string[] args)  
15.        {  
16.            MyObject obj = new MyObject();  
17.            obj.Birthday = new DateTime(1979, 11, 7);  
18.            obj.HomePlace = "湖北";  
19.            obj.Name = "周公";  
20.            Console.WriteLine("========使用BinaryFormatter类进行序列化和反序列化。====");  
21.            BinarySerialize(obj);  
22.            BinaryDeserialize("C:\\MyObject.dat");  
23.            Console.WriteLine("========使用SoapFormatter类进行序列化和反序列化。====");  
24.            SOAPSerialize(obj);  
25.            SOAPDeserialize("C:\\MyObject.soap");  
26.            Console.WriteLine("========使用XmlSerializer类进行序列化和反序列化。====");  
27.            XMLSerialize(obj);  
28.            XMLDeserialize("C:\\MyObject.xml");  
29.        }  
30.        /// <summary>  
31.        /// 二进制序列化对象  
32.        /// </summary>  
33.        /// <param name="obj"></param>  
34.        public static void BinarySerialize(MyObject obj)  
35.        {  
36.            using (FileStream stream = new FileStream("C:\\MyObject.dat", FileMode.Create, FileAccess.Write))  
37.            {  
38.                BinaryFormatter formater = new BinaryFormatter();  
39.                formater.Serialize(stream, obj);  
40.                Console.WriteLine("对象已经被序列化。" + obj.ToString());  
41.            }  
42.        }  
43.        /// <summary>  
44.        /// 二进制反序列化  
45.        /// </summary>  
46.        /// <param name="fileName"></param>  
47.        public static void BinaryDeserialize(string fileName)  
48.        {  
49.            using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read))  
50.            {  
51.                BinaryFormatter formater = new BinaryFormatter();  
52.                MyObject obj=(MyObject)formater.Deserialize(stream);  
53.                Console.WriteLine("对象已经被反序列化。" + obj.ToString());  
54.            }  
55.        }  
56. 
57.        /// <summary>  
58.        /// 二进制序列化对象  
59.        /// </summary>  
60.        /// <param name="obj"></param>  
61.        public static void SOAPSerialize(MyObject obj)  
62.        {  
63.            using (FileStream stream = new FileStream("C:\\MyObject.soap", FileMode.Create, FileAccess.Write))  
64.            {  
65.                SoapFormatter formater = new SoapFormatter();  
66.                formater.Serialize(stream, obj);  
67.                Console.WriteLine("对象已经被序列化。" + obj.ToString());  
68.            }  
69.        }  
70.        /// <summary>  
71.        /// 二进制反序列化  
72.        /// </summary>  
73.        /// <param name="fileName"></param>  
74.        public static void SOAPDeserialize(string fileName)  
75.        {  
76.            using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read))  
77.            {  
78.                SoapFormatter formater = new SoapFormatter();  
79.                MyObject obj = (MyObject)formater.Deserialize(stream);  
80.                Console.WriteLine("对象已经被反序列化。" + obj.ToString());  
81.            }  
82.        }  
83.        /// <summary>  
84.        /// XML序列化  
85.        /// </summary>  
86.        /// <param name="obj"></param>  
87.        public static void XMLSerialize(MyObject obj)  
88.        {  
89.            using (FileStream stream = new FileStream("C:\\MyObject.xml", FileMode.Create, FileAccess.Write))  
90.            {  
91.                XmlSerializer serializer = new XmlSerializer(typeof(MyObject));  
92.                serializer.Serialize(stream, obj);  
93.                Console.WriteLine("对象已经被序列化。" + obj.ToString());  
94.            }  
95.        }  
96.        /// <summary>  
97.        /// XML反序列化  
98.        /// </summary>  
99.        /// <param name="fileName"></param>  
100.        public static void XMLDeserialize(string fileName)  
101.        {  
102.            using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read))  
103.            {  
104.                XmlSerializer serializer = new XmlSerializer(typeof(MyObject));  
105.                MyObject obj = (MyObject)serializer.Deserialize(stream);  
106.                Console.WriteLine("对象已经被反序列化。" + obj.ToString());  
107.            }  
108.        }  
109.    }  
110.} 
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;
using System.Xml.Serialization;

namespace MySerializeDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            MyObject obj = new MyObject();
            obj.Birthday = new DateTime(1979, 11, 7);
            obj.HomePlace = "湖北";
            obj.Name = "周公";
            Console.WriteLine("========使用BinaryFormatter类进行序列化和反序列化。====");
            BinarySerialize(obj);
            BinaryDeserialize("C:\\MyObject.dat");
            Console.WriteLine("========使用SoapFormatter类进行序列化和反序列化。====");
            SOAPSerialize(obj);
            SOAPDeserialize("C:\\MyObject.soap");
            Console.WriteLine("========使用XmlSerializer类进行序列化和反序列化。====");
            XMLSerialize(obj);
            XMLDeserialize("C:\\MyObject.xml");
        }
        /// <summary>
        /// 二进制序列化对象
        /// </summary>
        /// <param name="obj"></param>
        public static void BinarySerialize(MyObject obj)
        {
            using (FileStream stream = new FileStream("C:\\MyObject.dat", FileMode.Create, FileAccess.Write))
            {
                BinaryFormatter formater = new BinaryFormatter();
                formater.Serialize(stream, obj);
                Console.WriteLine("对象已经被序列化。" + obj.ToString());
            }
        }
        /// <summary>
        /// 二进制反序列化
        /// </summary>
        /// <param name="fileName"></param>
        public static void BinaryDeserialize(string fileName)
        {
            using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
            {
                BinaryFormatter formater = new BinaryFormatter();
                MyObject obj=(MyObject)formater.Deserialize(stream);
                Console.WriteLine("对象已经被反序列化。" + obj.ToString());
            }
        }

        /// <summary>
        /// 二进制序列化对象
        /// </summary>
        /// <param name="obj"></param>
        public static void SOAPSerialize(MyObject obj)
        {
            using (FileStream stream = new FileStream("C:\\MyObject.soap", FileMode.Create, FileAccess.Write))
            {
                SoapFormatter formater = new SoapFormatter();
                formater.Serialize(stream, obj);
                Console.WriteLine("对象已经被序列化。" + obj.ToString());
            }
        }
        /// <summary>
        /// 二进制反序列化
        /// </summary>
        /// <param name="fileName"></param>
        public static void SOAPDeserialize(string fileName)
        {
            using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
            {
                SoapFormatter formater = new SoapFormatter();
                MyObject obj = (MyObject)formater.Deserialize(stream);
                Console.WriteLine("对象已经被反序列化。" + obj.ToString());
            }
        }
        /// <summary>
        /// XML序列化
        /// </summary>
        /// <param name="obj"></param>
        public static void XMLSerialize(MyObject obj)
        {
            using (FileStream stream = new FileStream("C:\\MyObject.xml", FileMode.Create, FileAccess.Write))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(MyObject));
                serializer.Serialize(stream, obj);
                Console.WriteLine("对象已经被序列化。" + obj.ToString());
            }
        }
        /// <summary>
        /// XML反序列化
        /// </summary>
        /// <param name="fileName"></param>
        public static void XMLDeserialize(string fileName)
        {
            using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(MyObject));
                MyObject obj = (MyObject)serializer.Deserialize(stream);
                Console.WriteLine("对象已经被反序列化。" + obj.ToString());
            }
        }
    }
}
这个程序的运行效果如下:
 
可见通过上面三个类都能实现将对象序列化保存,并且都能反序列化还原到对象被序列化之前的状态(这正是序列化意义所在,能保存对象运行时的状态并且还能还原)。如果运行上面的代码会在C盘根目录下创建三个文件,分别是MyObject.dat、MyObject.soap和MyObject.xml文件,因为MyObject.dat是二进制文件,所以无法查看文件的内容,但是我们可以打开MyObject.soap和MyObject.xml这两个文件来比较一下有什么区别。
MyObject.soap文件的后缀虽然是.soap,但是还是可以用记事本打开的,下面是MyObject.soap文件的内容:

view plaincopy to clipboardprint?
01.<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/"> 
02.<SOAP-ENV:Body> 
03.<a1:MyObject id="ref-1" xmlns:a1="http://schemas.microsoft.com/clr/nsassem/MySerializeDemo/MySerializeDemo%2C%20Version%3D1.0.0.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull"> 
04.<name id="ref-3">周公</name> 
05.<birthday>1979-11-07T00:00:00.0000000+08:00</birthday> 
06.<homePlace id="ref-4">湖北</homePlace> 
07.</a1:MyObject> 
08.</SOAP-ENV:Body> 
09.</SOAP-ENV:Envelope> 
<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:MyObject id="ref-1" xmlns:a1="http://schemas.microsoft.com/clr/nsassem/MySerializeDemo/MySerializeDemo%2C%20Version%3D1.0.0.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull">
<name id="ref-3">周公</name>
<birthday>1979-11-07T00:00:00.0000000+08:00</birthday>
<homePlace id="ref-4">湖北</homePlace>
</a1:MyObject>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
MyObject.xml文件也可以用记事本打开,它的内容如下:

view plaincopy to clipboardprint?
01.<?xml version="1.0"?> 
02.<MyObject xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
03.  <HomePlace>湖北</HomePlace> 
04.  <Birthday>1979-11-07T00:00:00</Birthday> 
05.  <Name>周公</Name> 
06.</MyObject> 
<?xml version="1.0"?>
<MyObject xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <HomePlace>湖北</HomePlace>
  <Birthday>1979-11-07T00:00:00</Birthday>
  <Name>周公</Name>
</MyObject>
熟悉SOAP协议的朋友一看MyObject.soap文件的内容就知道它符合SOAP协议,MyObject.xml文件毫无疑问是一个符合XML规范的文件。

对代码作几点说明:
1、如果采用BinaryFormatter类或者SoapFormatter类来实现序列化,则一定要给类加上Serializable属性,如代码中所示:

view plaincopy to clipboardprint?
01.[Serializable]  
02.    /// <summary>  
03.    /// 要序列化的对象  
04.    /// 作者:周公  
05.    /// 编写时间:2009-03-10  
06.    /// </summary>  
07.    public class MyObject 
[Serializable]
    /// <summary>
    /// 要序列化的对象
    /// 作者:周公
    /// 编写时间:2009-03-10
    /// </summary>
    public class MyObject
如果不给要序列化的对象加上这个属性,那么采用采用BinaryFormatter类或者SoapFormatter类来实现序列化时会报异常,但使用XmlSerializer 类序列化对象时可以不用这个属性。
2、另外,如果不想序列化某个字段,可以给其加上NonSerialized属性,这样在序列化时就不会保存这个这个字段的值了,比如不想序列化name这个字段,可以如下写代码:

view plaincopy to clipboardprint?
01.…//其它代码  
02.//[NonSerialized]  
03.private string name;  
04.…//其它代码 
…//其它代码
//[NonSerialized]
private string name;
…//其它代码
再次运行刚才的程序会得到如下效果:
 
看有黄色底线部分,因为name字段不被序列化,所以通过二进制序列化和SOAP序列化之后再反序化就得不到原来的值了。
3、最后还需要说明一点的是,SoapFormatter类在.net3.5开始已经过时了,微软建议使用BinaryFormatter类来序列化和反序列化了。

周公
2009-03-11 0:17

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/zhoufoxcn/archive/2009/03/11/3978874.aspx