文档详情

XML与DataTableDataSet互转(C#) 把数据库中表的内容转存为XML文件.doc

发布:2018-02-28约1.32万字共10页下载文档
文本预览下载声明
XML与DataTable/DataSet互转(C#) 把数据库中表的内容转存为XML文件 /**//// summary ??? /// 把DataSet、DataTable、DataView格式转换成XML字符串、XML文件 ??? /// /summary ??? public class DataToXml ??? { ??????? /**//// summary ??????? /// 将DataTable对象转换成XML字符串 ??????? /// /summary ??????? /// param name=dtDataTable对象/param ??????? /// returnsXML字符串/returns ??????? public static string CDataToXml(DataTable dt) ??????? { ??????????? if (dt != null) ??????????? { ??????????????? MemoryStream ms = null; ??????????????? XmlTextWriter XmlWt = null; ??????????????? try ??????????????? { ??????????????????? ms = new MemoryStream(); ??????????????????? //根据ms实例化XmlWt ??????????????????? XmlWt = new XmlTextWriter(ms, Encoding.Unicode); ??????????????????? //获取ds中的数据 ??????????????????? dt.WriteXml(XmlWt); ??????????????????? int count = (int)ms.Length; ??????????????????? byte[] temp = new byte[count]; ??????????????????? ms.Seek(0, SeekOrigin.Begin); ??????????????????? ms.Read(temp, 0, count); ??????????????????? //返回Unicode编码的文本 ??????????????????? UnicodeEncoding ucode = new 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 ; ??????????? } ??????? } ??????? /**//// summary ??????? /// 将DataSet对象中指定的Table转换成XML字符串 ??????? /// /summary ??????? /// param name=dsDataSet对象/param ??????? /// param name=tableIndexDataSet对象中的Table索引/param ??????? /// returnsXML字符串/returns ??????? public static string CDataToXml(DataSet ds, int tableIndex) ??????? { ??????????? if (tabl
显示全部
相似文档