datatable的序列化和反序列化 webservice
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /// 序列化DataTable
2 /// </summary>
3 /// <param name="pDt">包含数据的DataTable</param>
4 /// <returns>序列化的DataTable</returns>
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 /// <summary>
16 /// 反序列化DataTable
17 /// </summary>
18 /// <param name="pXml">序列化的DataTable</param>
19 /// <returns>DataTable</returns>
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 }