日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢(xún)
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
深入探討C#序列化和反序列化

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

創(chuàng)新互聯(lián)服務(wù)項(xiàng)目包括山西網(wǎng)站建設(shè)、山西網(wǎng)站制作、山西網(wǎng)頁(yè)制作以及山西網(wǎng)絡(luò)營(yíng)銷(xiāo)策劃等。多年來(lái),我們專(zhuān)注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢(shì)、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,山西網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到山西省份的部分城市,未來(lái)相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!

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

C#序列化和反序列化1、使用BinaryFormatter進(jìn)行串行化

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

 
 
 
  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 的摘要說(shuō)明
  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 = "男";
  22. }

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

 
 
 
  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();
  22. }

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

C#序列化和反序列化2、使用SoapFormatter進(jìn)行串行化

和BinaryFormatter類(lèi)似,我們只需要做一下簡(jiǎn)單修改即可:

a.將using語(yǔ)句中的.Formatter.Binary改為.Formatter.Soap;

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

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

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

C#序列化和反序列化3、使用XmlSerializer進(jìn)行串行化

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

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

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

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

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

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

要序列化的類(lèi):

 
 
 
  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. public string Sex;
  27. public int Age = 31;
  28. public Course[] Courses;
  29. public Person()
  30. {
  31. }
  32. public Person(string Name)
  33. {
  34. name = Name;
  35. Sex = "男";
  36. }
  37. }
  38. [Serializable]
  39. public class Course
  40. {
  41. public string Name;
  42. [XmlIgnore]
  43. public string Description;
  44. public Course()
  45. {
  46. }
  47. public Course(string name, string description)
  48. {
  49. Name = name;
  50. Description = description;
  51. }
  52. }  

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

 
 
 
  1. public void XMLSerialize()
  2. {
  3. Person c = new Person("cyj");
  4. c.Courses = new Course[2];
  5. c.Courses[0] = new Course("英語(yǔ)", "交流工具");
  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();
  24. }

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

 
 
 
  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﹥英語(yǔ)﹤/Name﹥
  10.   ﹤Description﹥交流工具﹤/Description﹥
  11. ﹤/Course﹥
  12. ﹤Course﹥
  13.   ﹤Name﹥數(shù)學(xué)﹤/Name﹥
  14.   ﹤Description﹥自然科學(xué)﹤/Description﹥
  15. ﹤/Course﹥
  16.   ﹤/Courses﹥
  17.   ﹤Name﹥cyj﹤/Name﹥
  18. ﹤/Person﹥

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

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

C#序列化和反序列化之實(shí)現(xiàn)ISerializable的類(lèi):

 
 
 
  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 的摘要說(shuō)明
  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. }
  40. }

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);
  22. }

C#序列化和反序列化的深入探討就是一個(gè)體驗(yàn)和嘗試的過(guò)程,那么希望本文對(duì)你了解和學(xué)習(xí)C#序列化和反序列化有所幫助。


網(wǎng)頁(yè)名稱(chēng):深入探討C#序列化和反序列化
轉(zhuǎn)載來(lái)于:http://www.dlmjj.cn/article/dpcjceo.html