datatable序列化成xml,xml反序列化成datatable
引用命名空间
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Soap;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml;
using System.Xml.Serialization;
然后调用下面的过程
/// <summary>
/// 序列化成xml
/// </summary>
/// <param name="dt"></param>
/// <param name="dtname"></param>
/// <returns></returns>
public string SerialDataTableXml(DataTable dt, string dtname)
{
dt.TableName = dtname;
StringBuilder sb = new StringBuilder();
XmlWriter xw = XmlWriter.Create(sb);
XmlSerializer serializer=new XmlSerializer(typeof(DataTable));
serializer.Serialize(xw, dt);
xw.Close();
return sb.ToString();
}
/// <summary>
/// 反序列化成dataTable
/// </summary>
/// <param name="pXml"></param>
/// <returns></returns>
public DataTable DeSerialXmlToDataTable(string pXml)
{
StringReader strReader = new StringReader(pXml);
XmlReader xmlReader = XmlReader.Create(strReader);
XmlSerializer serializer = new XmlSerializer(typeof(DataTable));
DataTable dt = serializer.Deserialize(xmlReader) as DataTable;
return dt;
}
posted on 2011-01-05 15:31 lijinchang 阅读(715) 评论(0) 编辑 收藏 举报