View Code
1 /// 序列化DataTable 2 /// 3 /// 包含数据的DataTable 4 ///序列化的DataTable 5 public static string SerializeDataTableXml(DataTable pDt,string pdtname) { 6 // 序列化DataTable 7 pDt.TableName = pdtname; 8 StringBuilder sb = new StringBuilder(); 9 XmlWriter writer = XmlWriter.Create(sb); 10 XmlSerializer serializer = new XmlSerializer(typeof(DataTable)); 11 serializer.Serialize(writer, pDt); 12 writer.Close(); 13 return sb.ToString(); 14 } 15 ///16 /// 反序列化DataTable 17 /// 18 /// 序列化的DataTable 19 ///DataTable 20 public static DataTable DeserializeDataTable(string pXml) { 21 22 StringReader strReader = new StringReader(pXml); 23 XmlReader xmlReader = XmlReader.Create(strReader); 24 XmlSerializer serializer = new XmlSerializer(typeof(DataTable)); 25 26 DataTable dt = serializer.Deserialize(xmlReader) as DataTable; 27 return dt; 28 }