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;
 
/// 
/// Xml序列化与反序列化
/// 
publicclass XmlUtil
{
    #region 反序列化
    /// 
    /// 反序列化
    /// 
    /// 类型
    /// XML字符串
    /// 
    publicstatic objectDeserialize(Type type, stringxml)
    {
        try
        {
            using(StringReader sr = newStringReader(xml))
            {
                XmlSerializer xmldes =new XmlSerializer(type);
                returnxmldes.Deserialize(sr);
            }
        }
        catch(Exception e)
        {
 
            returnnull;
        }
    }
    /// 
    /// 反序列化
    /// 
    /// 
    /// 
    /// 
    publicstatic objectDeserialize(Type type, Stream stream)
    {
        XmlSerializer xmldes =new XmlSerializer(type);
        returnxmldes.Deserialize(stream);
    }
    #endregion
 
    #region 序列化
    /// 
    /// 序列化
    /// 
    /// 类型
    /// 对象
    /// 
    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
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);
  1. Xml转换到实体对象
Student stu2 = XmlUtil.Deserialize(typeof(Student), xml)as Student;
Console.Write(string.Format("名字:{0},年龄:{1}", stu2.Name, stu2.Age));
  1. DataTable转换到Xml
// 生成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);
  1. Xml转换到DataTable
// 反序列化
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");
}
  1. List转换到Xml
// 生成List对象用于测试
List list1 =new List(3);
 
list1.Add(newStudent() { Name = "okbase", Age = 10 });
list1.Add(newStudent() { Name = "csdn", Age = 15 });
// 序列化
xml = XmlUtil.Serializer(typeof(List), list1);
Console.Write(xml);
  1. Xml转换到List
List list2 = XmlUtil.Deserialize(typeof(List), xml)as List;
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节点。...
public string Id

[XmlArrayAttribute("Areas")] // 表现为Xml层次结构,根为Areas,其所属的每个该集合节点元素名为类名。
public Area[] Areas

[XmlElementAttribute("Area", IsNullable = false)] // 表现为水平结构的Xml节点。...
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为:



  
    
      PD001
      street 001
      street 002
    
    
      XH002
      street 003
      street 004
    
  

下面我们开始具体分析结果,其中包含一些很有用的结论和注意事项:

  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之间的结构映射,希望足够同学们对付日常工作。更深入的讨论会在后续的文章跟进。

你可能感兴趣的:(c# XML和实体类之间相互转换(序列化和反序列化))