日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

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

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
.NET對象的XML序列化和反序列化實例詳解

.NET對象的XML序列化和反序列化是如何實現(xiàn)的呢?通過下面實例中的xml schema 描述了一個簡單的人力資源信息,詳細(xì)向你介紹.NET對象的XML序列化和反序列化的實現(xiàn)過程其中包含了XML的大部分格式,如XML元素相互嵌套, XML元素既有元素值,又有屬性值。

沙坪壩ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為成都創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18980820575(備注:SSL證書合作)期待與您的合作!

XML序列化和反序列化實現(xiàn)1. 待序列化的類層次結(jié)構(gòu)

 
 
 
  1. [XmlRoot("humanResource")]  
  2. public class HumanResource  
  3. {  
  4. #region private data.  
  5. private int m_record = 0;  
  6. private Worker[] m_workers = null;  
  7. #endregion  
  8.    
  9. [XmlAttribute(AttributeName="record")]  
  10. public int Record  
  11. {  
  12. get { return m_record; }  
  13. set { m_record = value; }  
  14. }  
  15.    
  16. [XmlElement(ElementName="worker")]  
  17. public Worker[] Workers  
  18. {  
  19. get { return m_workers; }  
  20. set { m_workers = value; }  
  21. }  
  22. }  
  23.    
  24. public class Worker  
  25. {  
  26. #region private data.  
  27. private string m_number = null;  
  28. private InformationItem[] m_infoItems = null;  
  29. #endregion  
  30.    
  31. [XmlAttribute("number")]  
  32. public string Number  
  33. {  
  34. get { return m_number; }  
  35. set { m_number = value; }  
  36. }  
  37.    
  38. [XmlElement("infoItem")]  
  39. public InformationItem[] InfoItems  
  40. {  
  41. get { return m_infoItems; }  
  42. set { m_infoItems = value; }  
  43. }  
  44. }  
  45.    
  46. public class InformationItem  
  47. {  
  48. #region private data.  
  49. private string m_name = null;  
  50. private string m_value = null;  
  51. #endregion  
  52.    
  53. [XmlAttribute(AttributeName = "name")]  
  54. public string Name  
  55. {  
  56. get { return m_name; }  
  57. set { m_name = value; }  
  58. }  
  59.    
  60. [XmlText]  
  61. public string Value  
  62. {  
  63. get { return m_value; }  
  64. set { m_value = value; }  
  65. }  

XML序列化和反序列化實現(xiàn)2. 序列化生成的xml結(jié)構(gòu)

 
 
 
  1. ﹤?xml version="1.0" ?﹥  
  2. ﹤humanResource   
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  4. xmlns:xsd="http://www.w3.org/2001/XMLSchema" record="2"﹥  
  5. ﹤worker number="001"﹥  
  6.  ﹤infoItem name="name"﹥Michale﹤/infoItem﹥  
  7.  ﹤infoItem name="sex"﹥male﹤/infoItem﹥  
  8.  ﹤infoItem name="age"﹥25﹤/infoItem﹥  
  9. ﹤/worker﹥  
  10. ﹤worker number="002"﹥  
  11.  ﹤infoItem name="name"﹥Surce﹤/infoItem﹥  
  12.  ﹤infoItem name="sex"﹥male﹤/infoItem﹥  
  13.  ﹤infoItem name="age"﹥28﹤/infoItem﹥  
  14.    ﹤/worker﹥  
  15.  ﹤/humanResource﹥ 

XML序列化和反序列化實現(xiàn)3. 利用XmlSerializer類進(jìn)行序列化和反序列化的實現(xiàn)

一般利用緩存機制實現(xiàn)xml文件只解析一次。

 
 
 
  1. public sealed class ConfigurationManager  
  2. {  
  3. private static HumanResource m_humanResource = null;  
  4. private ConfigurationManager(){}  
  5.    
  6. public static HumanResource Get(string path)  
  7. {  
  8. if (m_humanResource == null)  
  9. {  
  10. FileStream fs = null;  
  11. try 
  12. {  
  13. XmlSerializer xs =   
  14. new XmlSerializer(typeof(HumanResource));  
  15. fs = new FileStream(path,   
  16. FileMode.Open, FileAccess.Read);  
  17. m_humanResource = (HumanResource)xs.Deserialize(fs);  
  18. fs.Close();  
  19. return m_humanResource;  
  20. }  
  21. catch 
  22. {  
  23. if (fs != null)  
  24. fs.Close();  
  25. throw new Exception("Xml deserialization failed!");  
  26. }  
  27.    
  28. }  
  29. else 
  30. {  
  31. return m_humanResource;  
  32. }  
  33. }  
  34.    
  35. public static void Set(  
  36. string path, HumanResource humanResource)  
  37. {  
  38. if (humanResource == null)  
  39. throw new Exception("Parameter humanResource is null!");  
  40.  
  41. FileStream fs = null;  
  42. try 
  43. {  
  44. XmlSerializer xs =   
  45. new XmlSerializer(typeof(HumanResource));  
  46. fs = new FileStream(  
  47. path, FileMode.Create, FileAccess.Write);  
  48. xs.Serialize(fs, humanResource);  
  49. m_humanResource = null;  
  50. fs.Close();  
  51. }  
  52. catch 
  53. {  
  54. if (fs != null)  
  55. fs.Close();  
  56. throw new Exception("Xml serialization failed!");  
  57. }  
  58. }  
  59. }  

XML序列化和反序列化實現(xiàn)的基本內(nèi)容就向你介紹到這里,希望對你了解和學(xué)習(xí)XML序列化和反序列化的實現(xiàn)有所幫助。

【編輯推薦】

  1. 淺析C# XML解析實例
  2. C# XML解析方法實戰(zhàn)剖析
  3. C# XML解析方式實例解析
  4. 簡述C# XML解析方法的特點及應(yīng)用
  5. .NET對象的XML序列化和反序列化概念淺析

網(wǎng)站標(biāo)題:.NET對象的XML序列化和反序列化實例詳解
URL鏈接:http://www.5511xx.com/article/dhooddp.html