序列化及反序列化
将dataset序列化xml,用于webservice中
public static string DataSetToXmlString(DataTable Data) { using (MemoryStream stream = new MemoryStream()) { Data.WriteXml(stream); stream.Position = 0; using (TextReader reader = new StreamReader(stream)) { return reader.ReadToEnd(); } } }
public static string Dtb2Json(DataTable dtb) { JavaScriptSerializer jss = new JavaScriptSerializer(); System.Collections.ArrayList dic = new System.Collections.ArrayList(); foreach (DataRow dr in dtb.Rows) { System.Collections.Generic.Dictionary<string, object> drow = new System.Collections.Generic.Dictionary<string, object>(); foreach (DataColumn dc in dtb.Columns) { drow.Add(dc.ColumnName, dr[dc.ColumnName]); } dic.Add(drow); } //序列化 return jss.Serialize(dic); }