序列化和還原序列化

來源:互聯網
上載者:User

標籤:

一、介紹

    1.序列化和還原序列化,我們可能經常會聽到,其實通俗一點的解釋,序列化就是把一個對象儲存到一個檔案或資料庫欄位中去,

       還原序列化就是在適當的時候把這個檔案再轉化成原來的對象使用。

    2.把對象轉換為位元組序列的過程稱為對象的序列化。
   把位元組序列恢複為對象的過程稱為對象的還原序列化。

    3.序列化和還原序列化的作用 

  1>在進程下次啟動時讀取上次儲存的對象的資訊 2>、在不同的AppDomain或進程之間傳遞資料 3>、在分布式應用系統中傳遞資料 

  4.常見的序列化方法

 1>、BinaryFormatter  2>、SoapFormatter  3>、XML序列化

  5.BinaryFormatter  的用法

//BinaryFormatter將對象序列化到檔案中List<string> inputList = new List<string>() using (FileStream fsWriter = new FileStream(@"gz168168.tmp",FileMode.Create,FileAccess.Write))//存入的檔案資料(序列化對象){       BinaryFormatter bf = new BinaryFormatter();//       //序列化       bf.Serialize(fsWriter, inputList);}//BinaryFormatter將檔案中的資料還原序列化出來List<string> outputList = new List<string>();using (FileStream fsReader = new FileStream(@"gz168168.tmp",FileMode.Open,FileAccess.Read)){       BinaryFormatter bf = new BinaryFormatter();       //還原序列化       outputList = (List<string>)bf.Deserialize(fsReader);}

    6.MXL序列化的用法

List<string> inputList = new List<string>() using (FileStream fsWriter = new FileStream(@"test.xml",FileMode.Create,FileAccess.Write))//存入的檔案資料(序列化對象){      XmlSerializer xs = new XmlSerializer(typeof(List<string>));      xs.Serialize(fsWriter, inputList);}//從test.xml檔案中還原序列化出來List<string> outputList = new List<string>();using (FileStream fsReader = new FileStream(@"test.xml",FileMode.Open,FileAccess.Read)){     XmlSerializer xs = new XmlSerializer(typeof(List<string>));     outputList = xs.Deserialize(fsReader) as List<string>;}

   7.序列化的優點

二進位序列化的優點:   1. 所有的類成員(包括唯讀)都可以被序列化;  2. 效能非常好。XML序列化的優點:  1. 互通性好;  2. 不需要嚴格的二進位依賴;  3. 可讀性強

 

 8.總結

序列化         1.得到一個儲存物件的類型  2.建立一個寫入檔案流  3.定義要序列化的類型  4.調用序列化方法還原序列化:  1.定義一個裝載對象的類型  2.建立一個讀出檔案流  3.定義要還原序列化的類型  4.調用還原序列化方法

  

 

序列化和還原序列化

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.