2011年11月25日 星期五

DataTable的序列化和反序列化



///
/// 序列化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;
}

沒有留言:

張貼留言