1、实体类与XML相互转换
将实体类转换成XML需要使用XmlSerializer类的Serialize方法,将实体类序列化。
把XML转换成相应的实体类,需要使用到XmlSerializer类的Deserialize方法,将XML进行反序列化。
创建XML序列化公共处理类(XmlSerializeHelper.cs)
using System;
using System.IO;
using System.Xml.Serialization;
/// <summary>
/// XML序列化公共处理类
/// </summary>
public static class XmlSerializeHelper
{
/// <summary>
/// 将实体对象转换成XML
/// </summary>
/// <typeparam>实体类型</typeparam>
/// <param>实体对象</param>
public static string XmlSerialize<T>(T obj)
{
try
{
using (StringWriter sw = new StringWriter())
{
Type t = obj.GetType();
XmlSerializer serializer = new XmlSerializer(obj.GetType());
serializer.Serialize(sw, obj);
sw.Close();
return sw.ToString();
}
}
catch (Exception ex)
{
throw new Exception("将实体对象转换成XML异常", ex);
}
}
/// <summary>
/// 将XML转换成实体对象
/// </summary>
/// <typeparam>实体类型</typeparam>
/// <param>XML</param>
public static T DESerializer<T>(string strXML) where T : class
{
try
{
using (StringReader sr = new StringReader(strXML))
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
return serializer.Deserialize(sr) as T;
}
}
catch (Exception ex)
{
throw new Exception("将XML转换成实体对象异常", ex);
}
}
}
创建用户信息类,用于示例使用。
/// <summary>
/// 用户信息类
/// </summary>
public class UserInfo
{
/// <summary>
/// 编号
/// </summary>
public int ID { get; set; }
/// <summary>
/// 名称
/// </summary>
public string Name { get; set; }
/// <summary>
/// 创建时间
/// </summary>
public DateTime? CreateTime { get; set; }
}
1.1 示例1:将List与XML相互转换
/// <summary>
/// 将List与XML相互转换
/// </summary>
public static void ListToXmlTest()
{
//获取用户列表
List<UserInfo> userList = GetUserList();
//将实体对象转换成XML
string xmlResult = XmlSerializeHelper.XmlSerialize(userList);
//将XML转换成实体对象
List<UserInfo> deResult = XmlSerializeHelper.DESerializer<List<UserInfo>>(xmlResult);
}
/// <summary>
/// 获取用户信息列表
/// </summary>
public static List<UserInfo> GetUserList()
{
List<UserInfo> userList = new List<UserInfo>();
userList.Add(new UserInfo() { ID = 1, Name = "张三", CreateTime = DateTime.Now });
userList.Add(new UserInfo() { ID = 2, Name = "李四", CreateTime = DateTime.Now });
userList.Add(new UserInfo() { ID = 2, Name = "王五" });
return userList;
}
XML结果:
<?xml version="1.0" encoding="utf-16"?>
<ArrayOfUserInfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<UserInfo>
<ID>1</ID>
<Name>张三</Name>
<CreateTime>2018-10-04T15:59:53.7761027+08:00</CreateTime>
</UserInfo>
<UserInfo>
<ID>2</ID>
<Name>李四</Name>
<CreateTime>2018-10-04T15:59:54.9571044+08:00</CreateTime>
</UserInfo>
<UserInfo>
<ID>2</ID>
<Name>王五</Name>
<CreateTime xsi:nil="true" />
</UserInfo>
</ArrayOfUserInfo>
1.2 示例2:将DataTable与XML相互转换
/// <summary>
/// 将DataTable与XML相互转换
/// </summary>
public static void DataTableToXmlTest()
{
//创建DataTable对象
DataTable dt = CreateDataTable();
//将DataTable转换成XML
string xmlResult = XmlSerializeHelper.XmlSerialize(dt);
//将XML转换成DataTable
DataTable deResult = XmlSerializeHelper.DESerializer<DataTable>(xmlResult);
}
/// <summary>
/// 创建DataTable对象
/// </summary>
public static DataTable CreateDataTable()
{
//创建DataTable
DataTable dt = new DataTable("NewDt");
//创建自增长的ID列
DataColumn dc = dt.Columns.Add("ID", Type.GetType("System.Int32"));
dt.Columns.Add(new DataColumn("Name", Type.GetType("System.String")));
dt.Columns.Add(new DataColumn("CreateTime", Type.GetType("System.DateTime")));
//创建数据
DataRow dr = dt.NewRow();
dr["ID"] = 1;
dr["Name"] = "张三";
dr["CreateTime"] = DateTime.Now;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["ID"] = 2;
dr["Name"] = "李四";
dr["CreateTime"] = DateTime.Now;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["ID"] = 3;
dr["Name"] = "王五";
dr["CreateTime"] = DateTime.Now;
dt.Rows.Add(dr);
return dt;
}
XML结果: