2008年3月13日 星期四

DataTable的序列化和反序列化

來源網址:http://www.cnblogs.com/moonspell/archive/2007/07/17/821201.html
///
/// 序列化DataTable
///

/// DataTable
/// 序列化的DataTable
private static string SerializeDataTableXml(DataTable pDt) {
// 序列化DataTable
StringBuilder sb = new StringBuilder();
XmlWriter writer = XmlWriter.Create(sb);
XmlSerializer serializer = new XmlSerializer(typeof(DataTable));
serializer.Serialize(writer, pDt);
writer.Close();
return sb.ToString();
}

///
/// 反序列化DataTable
///

/// 序列化的DataTable
/// DataTable
public static DataTable DeserializeDataTable(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;
}

沒有留言: