c# XML和实体类之间相互转换(序列化和反序列化)
我们需要在XML与实体类,DataTable,List之间进行转换,下面是XmlUtil类,该类来自网络并稍加修改。
using
System;using
System.Collections.Generic;using
System.Linq;using
System.Text;using
System.IO;using
System.Data;using
System.Xml;using
System.Xml.Serialization;/// <summary>/// Xml序列化与反序列化/// </summary>publicclass
XmlUtil{ #region 反序列化 /// <summary> /// 反序列化 /// </summary> /// <param name="type">类型</param> /// <param name="xml">XML字符串</param> /// <returns></returns> publicstatic
objectDeserialize(Type type,
stringxml) { try { using(StringReader sr =
newStringReader(xml)) { XmlSerializer xmldes =new
XmlSerializer(type); returnxmldes.Deserialize(sr); } } catch(Exception e) { returnnull; } } /// <summary> /// 反序列化 /// </summary> /// <param name="type"></param> /// <param name="xml"></param> /// <returns></returns> publicstatic
objectDeserialize(Type type, Stream stream) { XmlSerializer xmldes =new
XmlSerializer(type); returnxmldes.Deserialize(stream); } #endregion #region 序列化 /// <summary> /// 序列化 /// </summary> /// <param name="type">类型</param> /// <param name="obj">对象</param> /// <returns></returns> publicstatic
stringSerializer(Type type,
objectobj) { MemoryStream Stream =new
MemoryStream(); XmlSerializer xml =new
XmlSerializer(type); try { //序列化对象 xml.Serialize(Stream, obj); } catch(InvalidOperationException) { throw; } Stream.Position = 0; StreamReader sr =new
StreamReader(Stream); stringstr = sr.ReadToEnd(); sr.Dispose(); Stream.Dispose(); returnstr; } #endregion}下面是测试代码:
1. 实体对象转换到Xml
|
1
2
3
4
5
6
7
8
9
|
public
class Student{ publicstring
Name { set;
get; } publicint
Age { set;get; }}Student stu1 =
new Student() { Name ="okbase", Age = 10 };string
xml = XmlUtil.Serializer(typeof(Student), stu1);Console.Write(xml); |
2. Xml转换到实体对象
|
1
2
|
Student stu2 = XmlUtil.Deserialize(typeof(Student), xml)as
Student;Console.Write(string.Format("名字:{0},年龄:{1}", stu2.Name,
stu2.Age)); |
3. DataTable转换到Xml
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
// 生成DataTable对象用于测试DataTable dt1 =
new DataTable("mytable"); // 必须指明DataTable名称dt1.Columns.Add("Dosage",typeof(int));dt1.Columns.Add("Drug",typeof(string));dt1.Columns.Add("Patient",typeof(string));dt1.Columns.Add("Date",typeof(DateTime));// 添加行dt1.Rows.Add(25,"Indocin",
"David", DateTime.Now);dt1.Rows.Add(50,"Enebrel",
"Sam", DateTime.Now);dt1.Rows.Add(10,"Hydralazine","Christoff", DateTime.Now);dt1.Rows.Add(21,"Combivent","Janet", DateTime.Now);dt1.Rows.Add(100,"Dilantin",
"Melanie", DateTime.Now);// 序列化xml = XmlUtil.Serializer(typeof(DataTable), dt1);Console.Write(xml); |
4. Xml转换到DataTable
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
// 反序列化DataTable dt2 = XmlUtil.Deserialize(typeof(DataTable), xml)as
DataTable;// 输出测试结果foreach(DataRow dr
indt2.Rows){ foreach(DataColumn col
indt2.Columns) { Console.Write(dr[col].ToString() +" "); } Console.Write("\r\n");} |
5. List转换到Xml
|
1
2
3
4
5
6
7
8
|
// 生成List对象用于测试List<Student> list1 =new
List<Student>(3);list1.Add(newStudent() { Name =
"okbase", Age = 10 });list1.Add(newStudent() { Name =
"csdn", Age = 15 });// 序列化xml = XmlUtil.Serializer(typeof(List<Student>), list1);Console.Write(xml); |
6. Xml转换到List
|
1
2
3
4
5
|
List<Student> list2 = XmlUtil.Deserialize(typeof(List<Student>), xml)as
List<Student>;foreach(Student stu
inlist2){ Console.WriteLine(stu.Name +","
+ stu.Age.ToString());} |
从代码可以看到,千变万化不离其宗!
==============================================================================
.Net Framework提供了对应的System.Xml.Seriazliation.XmlSerializer负责把对象序列化到XML,和从XML中反序列化为对象。Serializer的使用比较直观,需要多注意的是XML序列化相关的Attribute,怎么把这些attribute应用到我们的对象,以及对象公共属性上面去,生成满足预期格式的XML。
本文列出了最常用的方法和特性,涵盖日常大部分的转换工作,希望大家在工作中快速上手。为了给大家直观的印象,这里给出具体的使用代码,为了节省篇幅,代码异常处理没有添加,各位同学使用的时候酌情添加。
1. Serializer方法
下面的方法封装了XmlSerializer的调用,这里列出了参数最全的一个版本,具体使用的时候需适当添加重载:
public static class XmlSerializer { public static void SaveToXml(string filePath, object sourceObj, Type type, string xmlRootName) { if (!string.IsNullOrWhiteSpace(filePath) && sourceObj != null) { type = type != null ? type : sourceObj.GetType(); using (StreamWriter writer = new StreamWriter(filePath)) { System.Xml.Serialization.XmlSerializer xmlSerializer = string.IsNullOrWhiteSpace(xmlRootName) ? new System.Xml.Serialization.XmlSerializer(type) : new System.Xml.Serialization.XmlSerializer(type, new XmlRootAttribute(xmlRootName)); xmlSerializer.Serialize(writer, sourceObj); } } } public static object LoadFromXml(string filePath, Type type) { object result = null; if (File.Exists(filePath)) { using (StreamReader reader = new StreamReader(filePath)) { System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(type); result = xmlSerializer.Deserialize(reader); } } return result; } }
2. 序列化常用Attribute讲解说明:
[XmlRootAttribute("MyCity", Namespace="abc.abc", IsNullable=false)] // 当该类为Xml根节点时,以此为根节点名称。
public class City
[XmlAttribute("AreaName")] // 表现为Xml节点属性。<... AreaName="..."/>
public string Name
[XmlElementAttribute("AreaId", IsNullable = false)] // 表现为Xml节点。<AreaId>...</AreaId>
public string Id
[XmlArrayAttribute("Areas")] // 表现为Xml层次结构,根为Areas,其所属的每个该集合节点元素名为类名。<Areas><Area ... /><Area ... /></Areas>
public Area[] Areas
[XmlElementAttribute("Area", IsNullable = false)] // 表现为水平结构的Xml节点。<Area ... /><Area ... />...
public Area[] Areas
[XmlIgnoreAttribute] // 忽略该元素的序列化。
3. 详细举例说明
这里用简单的城市,区域和街区作为例子,具体示范一下上面的规则。
[XmlRootAttribute("MyCity", Namespace = "abc.abc", IsNullable = false)] public class City { [XmlAttribute("CityName")] public string Name { get; set; } [XmlAttribute("CityId")] public string Id { get; set; } [XmlArrayAttribute("Areas")] public Area[] Areas { get; set; } } [XmlRootAttribute("MyArea")] public class Area { [XmlAttribute("AreaName")] public string Name { get; set; } [XmlElementAttribute("AreaId", IsNullable = false)] public string Id { get; set; } [XmlElementAttribute("Street", IsNullable = false)] public string[] Streets { get; set; } }
根据以上类型,我们mock一些数据,然后用步骤1给出的Util方法输出:
static void Main(string[] args) { Area area1 = new Area(); area1.Name = "Pudong"; area1.Id = "PD001"; area1.Streets = new string [] { "street 001", "street 002" }; Area area2 = new Area(); area2.Name = "Xuhui"; area2.Id = "XH002"; area2.Streets = new string [] { "street 003", "street 004" }; City city1 = new City(); city1.Name = "Shanghai"; city1.Id = "SH001"; city1.Areas = new Area[] { area1, area2 }; XmlSerializer.SaveToXml(@"C:\temp\XML\output003.xml", city1); }
最终输出的XML为:
<?xml version="1.0" encoding="utf-8"?> <MyCity xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" CityName="Shanghai" CityId="SH001" xmlns="abc.abc"> <Areas> <Area AreaName="Pudong"> <AreaId>PD001</AreaId> <Street>street 001</Street> <Street>street 002</Street> </Area> <Area AreaName="Xuhui"> <AreaId>XH002</AreaId> <Street>street 003</Street> <Street>street 004</Street> </Area> </Areas> </MyCity>
下面我们开始具体分析结果,其中包含一些很有用的结论和注意事项:
1. xml的版本,编码,以及命名空间xmlns:xsi,xmlns:xsd为Framework自动添加。
2. 因为我们用City对象作为根节点,所以根节点名称为我们定义的"MyCity"。
但是,注意!这里指的是用City自身直接做根节点,如果是City集合比如City[],此时,该名称失效,系统会自动生成名称ArrayOfCity作为根节点名称(ArrayOf+类名),或者我们手动指定名称,这个就是在给大家的SaveToXml()方法中,参数xmlRootName的作用。
3. 如果以City为根节点并在XmlRootAttribute特性中给定名称,同时也手动指定了xmlRootName,系统会以手动指定的名称为准。
4. AreaName,AreaId,同为Area类的公共属性,一个被解释成属性,一个被解释成子节点。
Areas集合被解释成了层次结构,Streets集合被解释成了水平结构。
这两组区别最能体现不同序列化Attribute的用法。
4. 结语
这里用例子说明了Xml Serializer的用法,C#类和Xml之间的结构映射,希望足够同学们对付日常工作。更深入的讨论会在后续的文章跟进。
更多推荐



所有评论(0)