List实体类、DataTable与xml互转

序列化常用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]    // 忽略该元素的序列化。

参考

一、建立/XML序列化公共处理类

 /// <summary>
/// XML序列化公共处理类
/// </summary>
public static class XmlSerializeHelper
{
/// <summary>
/// 将实体对象转换成XML
/// </summary>
/// <typeparam name="T">实体类型</typeparam>
/// <param name="obj">实体对象</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 name="T">实体类型</typeparam>
/// <param name="strXML">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);
}
}
}

二、List实体互转调用事例

 static void Main(string[] args)
{
#region 测试数据
List<UserInfo> userList = new List<UserInfo>();
userList.Add(new UserInfo() { ID = , Name = "张三", CreateTime = DateTime.Now });
userList.Add(new UserInfo() { ID = , Name = "李四", CreateTime = DateTime.Now });
userList.Add(new UserInfo() { ID = , Name = "王五" });
#endregion
//将实体对象转换成XML
string xmlResult = XmlSerializeHelper.XmlSerialize(userList); //将XML转换成实体对象
List<UserInfo> deResult = XmlSerializeHelper.DESerializer<List<UserInfo>>(xmlResult);
}
/// <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; }
}

三、DataTable互转调用事列

 static void Main(string[] args)
{
#region 测试数据
//创建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"] = ;
dr["Name"] = "张三";
dr["CreateTime"] = DateTime.Now;
dt.Rows.Add(dr); dr = dt.NewRow();
dr["ID"] = ;
dr["Name"] = "李四";
dr["CreateTime"] = DateTime.Now;
dt.Rows.Add(dr); dr = dt.NewRow();
dr["ID"] = ;
dr["Name"] = "王五";
dr["CreateTime"] = DateTime.Now;
dt.Rows.Add(dr);
#endregion
//将DataTable转换成XML
string xmlResult = XmlSerializeHelper.XmlSerialize(dt); //将XML转换成DataTable
DataTable deResult = XmlSerializeHelper.DESerializer<DataTable>(xmlResult);
}
上一篇:Ibatis自动生成dao sqlmapper文件和domain文件过程


下一篇:'Invalid parameter not satisfying: body'