成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

深入C# 序列化(Serialize)、反序列化(Deserialize)

開發(fā) 后端
本文主要介紹了C#中的序列化和反序列化,序列化是.NET運(yùn)行時(shí)環(huán)境用來支持用戶定義類型的流化的機(jī)制,希望對(duì)你有幫助,一起來看吧。

 序列化又稱串行化,是.NET運(yùn)行時(shí)環(huán)境用來支持用戶定義類型的流化的機(jī)制。其目的是以某種存儲(chǔ)形成使自定義對(duì)象持久化,或者將這種對(duì)象從一個(gè)地方傳輸?shù)搅硪粋€(gè)地方。

.NET框架提供了兩種串行化的方式:

1、是使用BinaryFormatter進(jìn)行串行化;

2、使用SoapFormatter進(jìn)行串行化;

3、使用XmlSerializer進(jìn)行串行化。

***種方式提供了一個(gè)簡單的二進(jìn)制數(shù)據(jù)流以及某些附加的類型信息,而第二種將數(shù)據(jù)流格式化為XML存儲(chǔ);第三種其實(shí)和第二種差不多也是XML的格式存儲(chǔ),只不過比第二種的XML格式要簡化很多(去掉了SOAP特有的額外信息)。

可以使用[Serializable]屬性將類標(biāo)志為可序列化的。如果某個(gè)類的元素不想被序列化,1、2可以使用[NonSerialized]屬性來標(biāo)志,2、可以使用[XmlIgnore]來標(biāo)志。

1、使用BinaryFormatter進(jìn)行串行化

下面是一個(gè)可串行化的類:

  1. using System;  
  2. using System.Data;  
  3. using System.Configuration;  
  4. using System.Web;  
  5. using System.Web.Security;  
  6. using System.Web.UI;  
  7. using System.Web.UI.WebControls;  
  8. using System.Web.UI.WebControls.WebParts;  
  9. using System.Web.UI.HtmlControls;  
  10. using System.IO;  
  11. using System.Runtime.Serialization.Formatters.Binary;  
  12. /**//// <summary>  
  13. /// ClassToSerialize 的摘要說明  
  14. /// </summary>  
  15. [Serializable]  
  16. public class ClassToSerialize  
  17. {  
  18. public int id = 100;  
  19. public string name = "Name";  
  20. [NonSerialized]  
  21. public string Sex = "男";  

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

  1. public void SerializeNow()  
  2. {  
  3. ClassToSerialize c = new ClassToSerialize();  
  4. FileStream fileStream = new FileStream("c:\\temp.dat", FileMode.Create);  
  5. BinaryFormatter b = new BinaryFormatter();  
  6. b.Serialize(fileStream, c);  
  7. fileStream.Close();  
  8. }  
  9. public void DeSerializeNow()  
  10. {  
  11. ClassToSerialize c = new ClassToSerialize();  
  12. c.Sex = "kkkk";  
  13. FileStream fileStream = new FileStream("c:\\temp.dat", FileMode.Open, FileAccess.Read, FileShare.Read);  
  14. BinaryFormatter b = new BinaryFormatter();  
  15. c = b.Deserialize(fileStream) as ClassToSerialize;  
  16. Response.Write(c.name);  
  17. Response.Write(c.Sex);  
  18. fileStream.Close();  

調(diào)用上述兩個(gè)方法就可以看到串行化的結(jié)果:Sex屬性因?yàn)楸粯?biāo)志為[NonSerialized],故其值總是為null。

2、使用SoapFormatter進(jìn)行串行化

和BinaryFormatter類似,我們只需要做一下簡單修改即可:

a.將using語句中的.Formatter.Binary改為.Formatter.Soap;

b.將所有的BinaryFormatter替換為SoapFormatter.

c.確保報(bào)存文件的擴(kuò)展名為.xml

經(jīng)過上面簡單改動(dòng),即可實(shí)現(xiàn)SoapFormatter的串行化,這時(shí)候產(chǎn)生的文件就是一個(gè)xml格式的文件。

#p#

3、使用XmlSerializer進(jìn)行串行化

關(guān)于格式化器還有一個(gè)問題,假設(shè)我們需要XML,但是不想要SOAP特有的額外信息,那么我們應(yīng)該怎么辦呢?有兩中方案:要么編寫一個(gè)實(shí)現(xiàn)IFormatter接口的類,采用的方式類似于SoapFormatter類,但是沒有你不需要的信息;要么使用庫類XmlSerializer,這個(gè)類不使用Serializable屬性,但是它提供了類似的功能。

如果我們不想使用主流的串行化機(jī)制,而想使用XmlSeralizer進(jìn)行串行化我們需要做一下修改:

a.添加System.Xml.Serialization命名空間。

b.Serializable和NoSerialized屬性將被忽略,而是使用XmlIgnore屬性,它的行為與NoSerialized類似。

c.XmlSeralizer要求類有個(gè)默認(rèn)的構(gòu)造器,這個(gè)條件可能已經(jīng)滿足了。

下面看示例:

要序列化的類:

  1. using System;  
  2. using System.Data;  
  3. using System.Configuration;  
  4. using System.Web;  
  5. using System.Web.Security;  
  6. using System.Web.UI;  
  7. using System.Web.UI.WebControls;  
  8. using System.Web.UI.WebControls.WebParts;  
  9. using System.Web.UI.HtmlControls;  
  10. using System.Xml.Serialization;  
  11. [Serializable]  
  12. public class Person{  
  13. private string name;  
  14. public string Name{  
  15. get{  
  16. return name;  
  17. }  
  18. set{  
  19. name = value;  
  20. }}  
  21. public string Sex;  
  22. public int Age = 31;  
  23. public Course[] Courses;  
  24. public Person()  
  25. {}  
  26. public Person(string Name){  
  27. name = Name;  
  28. Sex = "男";  
  29. }  
  30. }  
  31. [Serializable]  
  32. public class Course{  
  33. public string Name;  
  34. [XmlIgnore]  
  35. public string Description;  
  36. public Course(){}  
  37. public Course(string name, string description){  
  38. Name = name;  
  39. Description = description;  
  40. }  
  41. }  

序列化和反序列化方法:

  1. public void XMLSerialize()  
  2. {  
  3. Person c = new Person("cyj");  
  4. c.Courses = new Course[2];  
  5. c.Courses[0] = new Course("英語""交流工具");  
  6. c.Courses[1] = new Course("數(shù)學(xué)","自然科學(xué)");  
  7. XmlSerializer xs = new XmlSerializer(typeof(Person));  
  8. Stream stream = new FileStream("c:\\cyj.XML",FileMode.Create,FileAccess.Write,FileShare.Read);  
  9. xs.Serialize(stream,c);  
  10. stream.Close();  
  11. }  
  12. public void XMLDeserialize()  
  13. {  
  14. XmlSerializer xs = new XmlSerializer(typeof(Person));  
  15. Stream stream = new FileStream("C:\\cyj.XML",FileMode.Open,FileAccess.Read,FileShare.Read);  
  16. Person p = xs.Deserialize(stream) as Person;  
  17. Response.Write(p.Name);  
  18. Response.Write(p.Age.ToString());  
  19. Response.Write(p.Courses[0].Name);  
  20. Response.Write(p.Courses[0].Description);  
  21. Response.Write(p.Courses[1].Name);  
  22. Response.Write(p.Courses[1].Description);  
  23. stream.Close();  

這里Course類的Description屬性值將始終為null,生成的xml文檔中也沒有該節(jié)點(diǎn),如下:

  1. <?xml version="1.0"?> 
  2. <Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
  3. <Sex></Sex> 
  4. <Age>31</Age> 
  5. <Courses> 
  6. <Course> 
  7. <Name>英語</Name> 
  8. <Description>交流工具</Description> 
  9. </Course> 
  10. <Course> 
  11. <Name>數(shù)學(xué)</Name> 
  12. <Description>自然科學(xué)</Description> 
  13. </Course> 
  14. </Courses> 
  15. <Name>cyj</Name> 
  16. </Person> 

#p#

4、自定義序列化

如果你希望讓用戶對(duì)類進(jìn)行串行化,但是對(duì)數(shù)據(jù)流的組織方式不完全滿意,那么可以通過在自定義類中實(shí)現(xiàn)接口來自定義串行化行為。這個(gè)接口只有一個(gè)方法,GetObjectData. 這個(gè)方法用于將對(duì)類對(duì)象進(jìn)行串行化所需要的數(shù)據(jù)填進(jìn)SerializationInfo對(duì)象。你使用的格式化器將構(gòu)造SerializationInfo對(duì)象,然后在串行化時(shí)調(diào)用GetObjectData. 如果類的父類也實(shí)現(xiàn)了ISerializable,那么應(yīng)該調(diào)用GetObjectData的父類實(shí)現(xiàn)。

如果你實(shí)現(xiàn)了ISerializable,那么還必須提供一個(gè)具有特定原型的構(gòu)造器,這個(gè)構(gòu)造器的參數(shù)列表必須與GetObjectData相同。這個(gè)構(gòu)造器應(yīng)該被聲明為私有的或受保護(hù)的,以防止粗心的開發(fā)人員直接使用它。

示例如下:

實(shí)現(xiàn)ISerializable的類:

  1. using System;  
  2. using System.Data;  
  3. using System.Configuration;  
  4. using System.Web;  
  5. using System.Web.Security;  
  6. using System.Web.UI;  
  7. using System.Web.UI.WebControls;  
  8. using System.Web.UI.WebControls.WebParts;  
  9. using System.Web.UI.HtmlControls;  
  10. using System.Runtime.Serialization;  
  11. using System.Runtime.Serialization.Formatters.Binary;  
  12. /**//// <summary>  
  13. /// Employee 的摘要說明  
  14. /// </summary>  
  15. [Serializable]  
  16. public class Employee:ISerializable  
  17. {  
  18. public int EmpId=100;  
  19. public string EmpName="劉德華";  
  20. [NonSerialized]  
  21. public string NoSerialString = "NoSerialString-Test";  
  22. public Employee()  
  23. {  
  24. //  
  25. // TODO: 在此處添加構(gòu)造函數(shù)邏輯  
  26. //  
  27. }  
  28. private Employee(SerializationInfo info, StreamingContext ctxt)  
  29. {  
  30. EmpId = (int)info.GetValue("EmployeeId"typeof(int));  
  31. EmpName = (String)info.GetValue("EmployeeName",typeof(string));  
  32. //NoSerialString = (String)info.GetValue("EmployeeString",typeof(string));  
  33. }  
  34. public void GetObjectData(SerializationInfo info, StreamingContext ctxt)  
  35. {  
  36. info.AddValue("EmployeeId", EmpId);  
  37. info.AddValue("EmployeeName", EmpName);  
  38. //info.AddValue("EmployeeString", NoSerialString);  
  39. }  

序列化和反序列化方法:

  1. public void OtherEmployeeClassTest()  
  2. {  
  3. Employee mp = new Employee();  
  4. mp.EmpId = 10;  
  5. mp.EmpName = "邱楓";  
  6. mp.NoSerialString = "你好呀";  
  7. Stream steam = File.Open("c:\\temp3.dat", FileMode.Create);  
  8. BinaryFormatter bf = new BinaryFormatter();  
  9. Response.Write("Writing Employee Info:");  
  10. bf.Serialize(steam,mp);  
  11. steam.Close();  
  12. mp = null;  
  13. //反序列化  
  14. Stream steam2 = File.Open("c:\\temp3.dat", FileMode.Open);  
  15. BinaryFormatter bf2 = new BinaryFormatter();  
  16. Response.Write("Reading Employee Info:");  
  17. Employee mp2 = (Employee)bf2.Deserialize(steam2);  
  18. steam2.Close();  
  19. Response.Write(mp2.EmpId);  
  20. Response.Write(mp2.EmpName);  
  21. Response.Write(mp2.NoSerialString);  

PS:本文章屬個(gè)人學(xué)習(xí)總結(jié),部分內(nèi)容參考互聯(lián)網(wǎng)上的相關(guān)文章。 其中如果發(fā)現(xiàn)個(gè)人總結(jié)有不正確的認(rèn)知或遺漏的地方請(qǐng)?jiān)u論告知,歡迎交流。

原文地址:http://www.cnblogs.com/qqflying/archive/2008/01/13/1037262.html

【編輯推薦】

  1. 用C#實(shí)現(xiàn)文件夾拷貝
  2. C#中自增、自減操作符重載是個(gè)怎么回事兒
  3. C#中的閉包是怎么捕獲變量的
  4. C#簡單游戲外掛制作(以Warcraft Ⅲ為例)
  5. 用C#實(shí)現(xiàn)HTTP協(xié)議下的多線程文件傳輸
責(zé)任編輯:于鐵 來源: 博客園
相關(guān)推薦

2009-08-24 17:14:08

C#序列化

2009-08-25 14:43:26

C#序列化和反序列化

2009-08-06 11:16:25

C#序列化和反序列化

2009-08-25 14:24:36

C#序列化和反序列化

2022-08-06 08:41:18

序列化反序列化Hessian

2011-06-01 15:05:02

序列化反序列化

2018-03-19 10:20:23

Java序列化反序列化

2011-05-18 15:20:13

XML

2023-12-13 13:49:52

Python序列化模塊

2024-01-30 13:32:51

JSON反序列化序列化

2021-10-20 07:18:50

Java 序列化漏洞

2019-11-20 10:07:23

web安全PHP序列化反序列化

2009-06-14 22:01:27

Java對(duì)象序列化反序列化

2024-03-05 12:49:30

序列化反序列化C#

2021-11-18 07:39:41

Json 序列化Vue

2009-09-09 16:10:11

.NET序列化和反序列

2012-04-13 10:45:59

XML

2015-05-08 12:41:36

C++序列化反序列化庫Kapok

2023-06-29 08:41:02

2024-05-06 00:00:00

C#序列化技術(shù)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)

主站蜘蛛池模板: 99热国产免费 | 亚洲成人av| 免费精品久久久久久中文字幕 | a在线视频 | 精品久久久久久久久久久下田 | 蜜臀久久 | 国产极品车模吞精高潮呻吟 | 国产成人综合在线 | 国产福利视频在线观看 | 欧美成人精品一区 | 亚洲黄色片免费观看 | 国产亚洲精品久久情网 | 亚洲精品自在在线观看 | 欧美一级大片 | 毛片软件 | 91久久北条麻妃一区二区三区 | 欧美亚洲综合久久 | 日日操日日舔 | 久婷婷 | 青青草这里只有精品 | 一区二区三区在线 | 久久99国产精一区二区三区 | 亚洲欧美日韩在线 | 91视视频在线观看入口直接观看 | 成人三区四区 | 99福利网 | 久久精品国产亚洲一区二区三区 | 在线免费观看日本 | 99精品一区二区三区 | 午夜天堂精品久久久久 | www.日本国产 | 精品福利在线 | 久久99久久98精品免观看软件 | 网址黄 | 国产精品日韩在线观看 | 午夜久久久久 | 欧美中文字幕一区 | 不卡在线视频 | 九九久久久| 国产日韩精品在线 | 一区二区三区在线观看视频 |