.NET对象的XML序列化和反序列化实例详解

开发 后端
.NET对象的XML序列化和反序列化实例实现主要向你介绍了一个完整的xml schema实例,向你详细的讲述了具体的操作步骤和使用方法,希望对你了解和学习XML序列化和反序列化有所帮助。

.NET对象的XML序列化和反序列化是如何实现的呢?通过下面实例中的xml schema 描述了一个简单的人力资源信息,详细向你介绍.NET对象的XML序列化和反序列化的实现过程其中包含了XML的大部分格式,如XML元素相互嵌套, XML元素既有元素值,又有属性值。

XML序列化和反序列化实现1. 待序列化的类层次结构

  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序列化和反序列化实现2. 序列化生成的xml结构

  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序列化和反序列化实现3. 利用XmlSerializer类进行序列化和反序列化的实现

一般利用缓存机制实现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序列化和反序列化实现的基本内容就向你介绍到这里,希望对你了解和学习XML序列化和反序列化的实现有所帮助。

【编辑推荐】

  1. 浅析C# XML解析实例
  2. C# XML解析方法实战剖析
  3. C# XML解析方式实例解析
  4. 简述C# XML解析方法的特点及应用
  5. .NET对象的XML序列化和反序列化概念浅析
责任编辑:仲衡 来源: 互联网
相关推荐

2009-09-09 14:45:41

XML序列化和反序列化

2011-06-01 15:05:02

序列化反序列化

2011-05-18 15:20:13

XML

2012-04-13 10:45:59

XML

2009-08-06 11:16:25

C#序列化和反序列化

2009-06-14 22:01:27

Java对象序列化反序列化

2018-03-19 10:20:23

Java序列化反序列化

2022-08-06 08:41:18

序列化反序列化Hessian

2009-08-24 17:14:08

C#序列化

2009-09-09 16:10:11

.NET序列化和反序列

2009-07-29 13:39:02

JSON序列化和反序列ASP.NET AJA

2011-06-01 14:26:11

序列化

2019-11-20 10:07:23

web安全PHP序列化反序列化

2009-08-25 14:24:36

C#序列化和反序列化

2023-12-13 13:49:52

Python序列化模块

2016-09-21 00:15:27

2021-11-18 07:39:41

Json 序列化Vue

2009-08-25 14:43:26

C#序列化和反序列化

2010-03-19 15:54:21

Java Socket

2011-06-01 14:50:48

点赞
收藏

51CTO技术栈公众号