1.DataTable turn XML
DataTableToXml #region /// <Summary> /// convert an object into XML string DataTable /// </ Summary> /// <param name = "DS"> the DataSet Object </ param> /// <Returns> XML string </ Returns> public static string DataTableToXml (the DataTable dt, string sName) { IF (dt = null!) { the MemoryStream ms = null; the XmlTextWriter XmlWt = null; the try { ms = the MemoryStream new new (); // The ms examples of XmlWt XmlWt the XmlTextWriter new new = (MS, System.Text.Encoding.Unicode); // Get the data ds dt.TableName = Sql.IsEmptyString(sName) ? "dt2xml" : sName; dt.WriteXml(XmlWt, XmlWriteMode.WriteSchema); int count = (int)ms.Length; byte[] temp = new byte[count]; ms.Seek(0, SeekOrigin.Begin); ms.Read(temp, 0, count); //返回Unicode编码的文本 System.Text.UnicodeEncoding ucode = new System.Text.UnicodeEncoding(); string returnValue = ucode.GetString(temp).Trim(); return returnValue; } catch (System.Exception ex) { throw ex; } finally { //释放资源 if (XmlWt != null) { XmlWt.Close(); ms.Close(); ms.Dispose(); } } } else { return ""; } } #endregion
2.XML turn DataSet
#region Xml To DataSet public static DataSet XmlToDataSet(string xmlString) { XmlDocument xmldoc = new XmlDocument(); xmldoc.LoadXml(xmlString); StringReader stream = null; XmlTextReader reader = null; try { DataSet xmlDS = new DataSet(); stream = new StringReader(xmldoc.InnerXml); reader = new XmlTextReader(stream); xmlDS.ReadXml(reader); reader.Close(); return xmlDS; } catch (System.Exception ex) { reader.Close(); throw ex; } } #endregion