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

深入探討C#序列化和反序列化

開發 后端
深入學習C#序列化和反序列化不僅僅向你介紹了C#序列化和反序列化各自的概念,而且向你展示了C#序列化和反序列化的具體體現及實例解析。

深入探討C#序列化和反序列化之前我們先要明白什么是序列化,它又稱串行化,是.NET運行時環境用來支持用戶定義類型的流化的機制。序列化就是把一個對象保存到一個文件或數據庫字段中去,反序列化就是在適當的時候把這個文件再轉化成原來的對象使用。其目的是以某種存儲形成使自定義對象持久化,或者將這種對象從一個地方傳輸到另一個地方。.NET框架提供了兩種串行化的方式:1、是使用BinaryFormatter進行串行化;2、使用SoapFormatter進行串行化;3、使用XmlSerializer進行串行化。***種方式提供了一個簡單的二進制數據流以及某些附加的類型信息,而第二種將數據流格式化為XML存儲;第三種其實和第二種差不多也是XML的格式存儲,只不過比第二種的XML格式要簡化很多(去掉了SOAP特有的額外信息)??梢允褂肹Serializable]屬性將類標志為可序列化的。如果某個類的元素不想被序列化,1、2可以使用[NonSerialized]屬性來標志,2、可以使用[XmlIgnore]來標志。

下面就讓我們開始深入了解C#序列化和反序列化:

C#序列化和反序列化1、使用BinaryFormatter進行串行化

下面是一個可串行化的類:

  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 =   
  5. new FileStream("c:\\temp.dat", FileMode.Create);  
  6. BinaryFormatter b = new BinaryFormatter();  
  7. b.Serialize(fileStream, c);  
  8. fileStream.Close();  
  9. }  
  10. public void DeSerializeNow()  
  11. {  
  12. ClassToSerialize c = new ClassToSerialize();  
  13. c.Sex = "kkkk";  
  14. FileStream fileStream =  
  15.  new FileStream("c:\\temp.dat",   
  16. FileMode.Open, FileAccess.Read, FileShare.Read);  
  17. BinaryFormatter b = new BinaryFormatter();  
  18. c = b.Deserialize(fileStream) as ClassToSerialize;  
  19.   Response.Write(c.name);  
  20. Response.Write(c.Sex);  
  21. fileStream.Close();  

調用上述兩個方法就可以看到串行化的結果:Sex屬性因為被標志為[NonSerialized],故其值總是為null。

C#序列化和反序列化2、使用SoapFormatter進行串行化

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

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

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

c.確保報存文件的擴展名為.xml

經過上面簡單改動,即可實現SoapFormatter的串行化,這時候產生的文件就是一個xml格式的文件。

C#序列化和反序列化3、使用XmlSerializer進行串行化

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

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

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

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

c.XmlSeralizer要求類有個默認的構造器,這個條件可能已經滿足了。

下面看C#序列化和反序列化示例:

要序列化的類:

  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. {  
  14. private string name;  
  15. public string Name  
  16. {  
  17. get 
  18. {  
  19. return name;  
  20. }  
  21. set 
  22. {  
  23. name = value;  
  24. }  
  25. }  
  26.  
  27.  
  28. public string Sex;  
  29. public int Age = 31;  
  30. public Course[] Courses;  
  31.  
  32. public Person()  
  33. {  
  34. }  
  35. public Person(string Name)  
  36. {  
  37. name = Name;  
  38. Sex = "男";  
  39. }  
  40. }  
  41. [Serializable]  
  42. public class Course  
  43. {  
  44. public string Name;  
  45. [XmlIgnore]  
  46. public string Description;  
  47. public Course()  
  48. {  
  49. }  
  50. public Course(string name, string description)  
  51. {  
  52. Name = name;  
  53. Description = description;  
  54. }  
  55. }    

C#序列化和反序列化方法:

  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("數學","自然科學");  
  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文檔中也沒有該節點,如下:

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

C#序列化和反序列化4、自定義序列化

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

C#序列化和反序列化之實現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: 在此處添加構造函數邏輯  
  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. }  

C#序列化和反序列化方法:

  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. //C#序列化和反序列化之反序列化  
  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);  

C#序列化和反序列化的深入探討就是一個體驗和嘗試的過程,那么希望本文對你了解和學習C#序列化和反序列化有所幫助。

【編輯推薦】

  1. 淺析C# Dictionary泛型集合
  2. C# 泛型編程基礎實例詳解
  3. 學習C#泛型集合類型的心得體會
  4. .net泛型類的學習總結
  5. 深度剖析C#序列化和反序列化
責任編輯:仲衡 來源: 博客園
相關推薦

2009-08-24 17:14:08

C#序列化

2011-06-01 14:50:48

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

序列化反序列化

2011-05-18 15:20:13

XML

2024-01-30 13:32:51

JSON反序列化序列化

2019-11-20 10:07:23

web安全PHP序列化反序列化

2009-06-14 22:01:27

Java對象序列化反序列化

2018-03-19 10:20:23

Java序列化反序列化

2023-12-13 13:49:52

Python序列化模塊

2021-11-18 07:39:41

Json 序列化Vue

2024-03-05 12:49:30

序列化反序列化C#

2009-09-09 16:10:11

.NET序列化和反序列

2021-10-20 07:18:50

Java 序列化漏洞

2009-09-09 14:45:41

XML序列化和反序列化

2009-09-09 15:47:27

XML序列化和反序列化

2010-03-19 15:54:21

Java Socket

2009-07-29 13:39:02

JSON序列化和反序列ASP.NET AJA
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 亚洲国产成人久久久 | 97在线观看 | 欧美一级在线免费观看 | 毛片一区二区三区 | 精品国产乱码久久久久久影片 | 成人国内精品久久久久一区 | 精品国产一区二区三区av片 | 国产精品五区 | 台湾a级理论片在线观看 | 天天干天天干 | 天天操夜夜看 | 精品九九在线 | 亚洲福利在线观看 | 国产精品毛片久久久久久 | 99精品视频免费在线观看 | а_天堂中文最新版地址 | 91人人澡人人爽 | 亚洲精品久久久久久一区二区 | 亚洲一区二区视频 | 亚洲精品视频免费观看 | 一级毛片成人免费看a | 97成人精品 | 国产999精品久久久久久 | 99re视频在线免费观看 | 黄色一级免费看 | 精品国产一区探花在线观看 | 午夜欧美一区二区三区在线播放 | 亚洲有码转帖 | 亚洲欧洲中文 | 国产又爽又黄的视频 | 中文字幕一区二区三区精彩视频 | 欧美日韩高清一区 | 久久精品欧美电影 | 午夜视频在线观看视频 | 国产成人午夜精品影院游乐网 | 一区二区三区四区电影 | 久久久精品在线 | 亚洲精品乱码久久久久久久久久 | www日韩欧美 | 成人小视频在线 | 国产在线观看一区二区 |