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

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

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

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

[XmlRoot("humanResource")]  
public class HumanResource  
{  
#region private data.  
private int m_record = 0;  
private Worker[] m_workers = null;  
#endregion  
   
[XmlAttribute(AttributeName="record")]  
public int Record  
{  
get { return m_record; }  
set { m_record = value; }  
}  
   
[XmlElement(ElementName="worker")]  
public Worker[] Workers  
{  
get { return m_workers; }  
set { m_workers = value; }  
}  
}  
   
public class Worker  
{  
#region private data.  
private string m_number = null;  
private InformationItem[] m_infoItems = null;  
#endregion  
   
[XmlAttribute("number")]  
public string Number  
{  
get { return m_number; }  
set { m_number = value; }  
}  
   
[XmlElement("infoItem")]  
public InformationItem[] InfoItems  
{  
get { return m_infoItems; }  
set { m_infoItems = value; }  
}  
}  
   
public class InformationItem  
{  
#region private data.  
private string m_name = null;  
private string m_value = null;  
#endregion  
   
[XmlAttribute(AttributeName = "name")]  
public string Name  
{  
get { return m_name; }  
set { m_name = value; }  
}  
   
[XmlText]  
public string Value  
{  
get { return m_value; }  
set { m_value = value; }  
}  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.

XML序列化和反序列化实现2. 序列化生成的xml结构

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

XML序列化和反序列化实现3. 利用XmlSerializer类进行序列化和反序列化的实现

一般利用缓存机制实现xml文件只解析一次。

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

2009-08-06 11:16:25

C#序列化和反序列化

2012-04-13 10:45:59

XML

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#序列化和反序列化

2016-09-21 00:15:27

2023-12-13 13:49:52

Python序列化模块

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技术栈公众号