C# 序列化與反序列化

一、概念

     把對象轉化爲字節序列的過程稱爲序列化

     把字節序列恢復爲對象的過程稱爲反序列化

 序列化又稱串行化,是.NET運行時環境用來支持用戶定義類型的流化的機制。其目的是以某種存儲形成使自定義對象持久化,或者將這種對象從一個地方傳輸到另一個地方。
    .NET框架提供了兩種串行化的方式:1、是使用BinaryFormatter進行串行化;2、使用SoapFormatter進行串行化;3、使用XmlSerializer進行串行化。第一種方式提供了一個簡單的二進制數據流以及某些附加的類型信息,而第二種將數據流格式化爲XML存儲;第三種其實和第二種差不多也是XML的格式存儲,只不過比第二種的XML格式要簡化很多(去掉了SOAP特有的額外信息)。
    可以使用[Serializable]屬性將類標誌爲可序列化的。如果某個類的元素不想被序列化,1、2可以使用[NonSerialized]屬性來標誌,2、可以使用[XmlIgnore]來標誌。
    1、使用BinaryFormatter進行串行化
    下面是一個可串行化的類:
    

using System;
using
 System.Data;
using
 System.Configuration;
using
 System.Web;
using
 System.Web.Security;
using
 System.Web.UI;
using
 System.Web.UI.WebControls;
using
 System.Web.UI.WebControls.WebParts;
using
 System.Web.UI.HtmlControls;
using
 System.IO;
using
 System.Runtime.Serialization.Formatters.Binary;
/**/
/// <summary>
/// ClassToSerialize 的摘要說明
///
 
</summary>

[Serializable]
public
 
class
 ClassToSerialize
{
    
public
 int id = 100
;
    
public
 string name = "Name"
;
    [NonSerialized]
    
public string Sex = ""
;
}

下面是串行化和反串行化的方法:

public 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 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;
          
Response.Write(c.name);
        Response.Write(c.Sex);
        fileStream.Close();
    }


    調用上述兩個方法就可以看到串行化的結果:Sex屬性因爲被標誌爲[NonSerialized],故其值總是爲null。
    
2、使用SoapFormatter進行串行化
    和BinaryFormatter類似,我們只需要做一下簡單修改即可:
    a.將using語句中的.Formatter.Binary改爲.Formatter.Soap;
    b.將所有的BinaryFormatter替換爲SoapFormatter.
    c.確保報存文件的擴展名爲.xml
    經過上面簡單改動,即可實現SoapFormatter的串行化,這時候產生的文件就是一個xml格式的文件。
    3、使用XmlSerializer進行串行化
    關於格式化器還有一個問題,假設我們需要XML,但是不想要SOAP特有的額外信息,那麼我們應該怎麼辦呢?有兩中方案:要麼編寫一個實現IFormatter接口的類,採用的方式類似於SoapFormatter類,但是沒有你不需要的信息;要麼使用庫類XmlSerializer,這個類不使用Serializable屬性,但是它提供了類似的功能。
    如果我們不想使用主流的串行化機制,而想使用XmlSeralizer進行串行化我們需要做一下修改:
    a.添加System.Xml.Serialization命名空間。
    b.Serializable和NoSerialized屬性將被忽略,而是使用XmlIgnore屬性,它的行爲與NoSerialized類似。
    c.XmlSeralizer要求類有個默認的構造器,這個條件可能已經滿足了。
    下面看示例:
    要序列化的類:
    

using System;
using
 System.Data;
using
 System.Configuration;
using
 System.Web;
using
 System.Web.Security;
using
 System.Web.UI;
using
 System.Web.UI.WebControls;
using
 System.Web.UI.WebControls.WebParts;
using
 System.Web.UI.HtmlControls;
using
 System.Xml.Serialization;
[Serializable]
public
 class
 Person
{
    
private
 string
 name;
    
public
 string
 Name
    
{
        
get

        
{
            
return
 name;
        }

        
set
        
{
            name 
=
 value;
        }

    }



    
public string Sex;
    
public
 int Age = 31
;
    
public
 Course[] Courses;

    
public
 Person()
    
{
    }

    
public Person(string Name)
    
{
        name 
=
 Name;
        Sex 
=
 ""
;
    }

}

[Serializable]
public class Course
{
    
public
 string
 Name;
    [XmlIgnore]
    
public string
 Description;
    
public
 Course()
    
{
    }

    
public Course(string name, string description)
    
{
        Name 
=
 name;
        Description 
=
 description;
    }

}
  


    序列化和反序列化方法:

public void XMLSerialize()
    
{
        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));
        Stream stream 
=
 new FileStream("c:\\cyj.XML"
,FileMode.Create,FileAccess.Write,FileShare.Read);
        xs.Serialize(stream,c);
        stream.Close();
    }

    
public void XMLDeserialize()
    
{
        XmlSerializer xs 
=
 new XmlSerializer(typeof
(Person));
        Stream stream 
=
 new FileStream("C:\\cyj.XML"
,FileMode.Open,FileAccess.Read,FileShare.Read);
        Person p 
=
 xs.Deserialize(stream) as
 Person;
        Response.Write(p.Name);
        Response.Write(p.Age.ToString());
        Response.Write(p.Courses[
0
].Name);
        Response.Write(p.Courses[
0
].Description);
        Response.Write(p.Courses[
1
].Name);
        Response.Write(p.Courses[
1
].Description);
        stream.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">
  
<Sex></Sex>
  
<Age>31</Age>
  
<Courses>
    
<Course>
      
<Name>英語</Name>
      
<Description>交流工具</Description>
    
</Course>
    
<Course>
      
<Name>數學</Name>
      
<Description>自然科學</Description>
    
</Course>
  
</Courses>
  
<Name>cyj</Name>
</Person>


    4、自定義序列化
    如果你希望讓用戶對類進行串行化,但是對數據流的組織方式不完全滿意,那麼可以通過在自定義類中實現接口來自定義串行化行爲。這個接口只有一個方法,GetObjectData. 這個方法用於將對類對象進行串行化所需要的數據填進SerializationInfo對象。你使用的格式化器將構造SerializationInfo對象,然後在串行化時調用GetObjectData. 如果類的父類也實現了ISerializable,那麼應該調用GetObjectData的父類實現。
    如果你實現了ISerializable,那麼還必須提供一個具有特定原型的構造器,這個構造器的參數列表必須與GetObjectData相同。這個構造器應該被聲明爲私有的或受保護的,以防止粗心的開發人員直接使用它。
    示例如下:
    實現ISerializable的類:
    

using System;
using
 System.Data;
using
 System.Configuration;
using
 System.Web;
using
 System.Web.Security;
using
 System.Web.UI;
using
 System.Web.UI.WebControls;
using
 System.Web.UI.WebControls.WebParts;
using
 System.Web.UI.HtmlControls;
using
 System.Runtime.Serialization;
using
 System.Runtime.Serialization.Formatters.Binary;
/**/
/// <summary>
/// Employee 的摘要說明
///
 
</summary>

[Serializable]
public
 
class
 Employee:ISerializable
{
    
public
 int EmpId=100
;
    
public
 string EmpName="劉德華"
;
    [NonSerialized]
    
public string NoSerialString = "NoSerialString-Test"
;
    
public
 Employee()
    
{
        
//

        
// TODO: 在此處添加構造函數邏輯
        
//
    }

    
private Employee(SerializationInfo info, StreamingContext ctxt)
    
{
        EmpId 
=
 (int)info.GetValue("EmployeeId"typeof(int
));
        EmpName 
=
 (String)info.GetValue("EmployeeName",typeof(string
));
        
//
NoSerialString = (String)info.GetValue("EmployeeString",typeof(string));

    }

    
public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
    
{
        info.AddValue(
"
EmployeeId"
, EmpId);
        info.AddValue(
"
EmployeeName"
, EmpName);
        
//
info.AddValue("EmployeeString", NoSerialString);

    }

}


    序列化和反序列化方法:

public void OtherEmployeeClassTest()
    
{
        Employee mp 
=
 new
 Employee();
        mp.EmpId 
=
 10
;
        mp.EmpName 
=
 "邱楓"
;
        mp.NoSerialString 
=
 "你好呀"
;
        Stream steam 
=
 File.Open("c:\\temp3.dat"
, FileMode.Create);
        BinaryFormatter bf 
=
 new
 BinaryFormatter();
        Response.Write(
"
Writing Employee Info:"
);
        bf.Serialize(steam,mp);
        steam.Close();
        mp 
= null
;
        
//
反序列化

        Stream steam2 = File.Open("c:\\temp3.dat", FileMode.Open);
        BinaryFormatter bf2 
=
 new
 BinaryFormatter();
        Response.Write(
"
Reading Employee Info:"
);
        Employee mp2 
=
 (Employee)bf2.Deserialize(steam2);
        steam2.Close();
        Response.Write(mp2.EmpId);
        Response.Write(mp2.EmpName);
        Response.Write(mp2.NoSerialString);
    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章