




已阅读5页,还剩17页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
DataSet(DataTable)与XML互转加引用:usingSystem;usingSystem.Data;usingSystem.IO;usingSystem.Xml;usingSystem.Text;/相应C#代码:privatestringConvertDataTableToXML(DataTablexmlDS)Memo.加引用: usingSystem; usingSystem.Data; usingSystem.IO; usingSystem.Xml; usingSystem.Text; /相应C#代码: privatestringConvertDataTableToXML(DataTablexmlDS) MemoryStreamstream=null; XmlTextWriterwriter=null; try stream=newMemoryStream(); writer=newXmlTextWriter(stream,Encoding.Default); xmlDS.WriteXml(writer); intcount=(int)stream.Length; bytearr=newbytecount; stream.Seek(0,SeekOrigin.Begin); stream.Read(arr,0,count); UTF8Encodingutf=newUTF8Encoding(); returnutf.GetString(arr).Trim(); catch returnString.Empty; finally if(writer!=null)writer.Close(); privateDataSetConvertXMLToDataSet(stringxmlData) StringReaderstream=null; XmlTextReaderreader=null; try DataSetxmlDS=newDataSet(); stream=newStringReader(xmlData); reader=newXmlTextReader(stream); xmlDS.ReadXml(reader); returnxmlDS; catch(Exceptionex) stringstrTest=ex.Message; returnnull; finally if(reader!=null) reader.Close(); XML与DataTable/DataSet互转(C#) 把数据库中表的内容转存为XML文件Xml 2009-09-24 13:47:25 阅读47 评论0 字号:大中小订阅/*/ / 把DataSet、DataTable、DataView格式转换成XML字符串、XML文件 / public class DataToXml /*/ / 将DataTable对象转换成XML字符串 / / DataTable对象 / XML字符串 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 bytecount; 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 ; /*/ / 将DataSet对象中指定的Table转换成XML字符串 / / DataSet对象 / DataSet对象中的Table索引 / XML字符串 public static string CDataToXml(DataSet ds, int tableIndex) if (tableIndex != -1) return CDataToXml(ds.TablestableIndex); else return CDataToXml(ds.Tables0); /*/ / 将DataSet对象转换成XML字符串 / / DataSet对象 / XML字符串 public static string CDataToXml(DataSet ds) return CDataToXml(ds, -1); /*/ / 将DataView对象转换成XML字符串 / / DataView对象 / XML字符串 public static string CDataToXml(DataView dv) return CDataToXml(dv.Table); /*/ / 将DataSet对象数据保存为XML文件 / / DataSet / XML文件路径 / bool值 public static bool CDataToXmlFile(DataTable dt, string xmlFilePath) if (dt != null) & (!string.IsNullOrEmpty(xmlFilePath) string path = HttpContext.Current.Server.MapPath(xmlFilePath); 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 bytecount; ms.Seek(0, SeekOrigin.Begin); ms.Read(temp, 0, count); /返回Unicode编码的文本 UnicodeEncoding ucode = new UnicodeEncoding(); /写文件 StreamWriter sw = new StreamWriter(path); sw.WriteLine(); sw.WriteLine(ucode.GetString(temp).Trim(); sw.Close(); return true; catch (System.Exception ex) throw ex; finally /释放资源 if (XmlWt != null) XmlWt.Close(); ms.Close(); ms.Dispose(); else return false; /*/ / 将DataSet对象中指定的Table转换成XML文件 / / DataSet对象 / DataSet对象中的Table索引 / xml文件路径 / bool值 public static bool CDataToXmlFile(DataSet ds, int tableIndex, string xmlFilePath) if (tableIndex != -1) return CDataToXmlFile(ds.TablestableIndex, xmlFilePath); else return CDataToXmlFile(ds.Tables0, xmlFilePath); /*/ / 将DataSet对象转换成XML文件 / / DataSet对象 / xml文件路径 / bool值 public static bool CDataToXmlFile(DataSet ds, string xmlFilePath) return CDataToXmlFile(ds, -1, xmlFilePath); /*/ / 将DataView对象转换成XML文件 / / DataView对象 / xml文件路径 / bool值 public static bool CDataToXmlFile(DataView dv, string xmlFilePath) return CDataToXmlFile(dv.Table, xmlFilePath); /*/ / XML形式的字符串、XML文江转换成DataSet、DataTable格式 / public class XmlToData /*/ / 将Xml内容字符串转换成DataSet对象 / / Xml内容字符串 / DataSet对象 public static DataSet CXmlToDataSet(string xmlStr) if (!string.IsNullOrEmpty(xmlStr) StringReader StrStream = null; XmlTextReader Xmlrdr = null; try DataSet ds = new DataSet(); /读取字符串中的信息 StrStream = new StringReader(xmlStr); /获取StrStream中的数据 Xmlrdr = new XmlTextReader(StrStream); /ds获取Xmlrdr中的数据 ds.ReadXml(Xmlrdr); return ds; catch (Exception e) throw e; finally /释放资源 if (Xmlrdr != null) Xmlrdr.Close(); StrStream.Close(); StrStream.Dispose(); else return null; /*/ / 将Xml字符串转换成DataTable对象 / / Xml字符串 / Table表索引 / DataTable对象 public static DataTable CXmlToDatatTable(string xmlStr, int tableIndex) return CXmlToDataSet(xmlStr).TablestableIndex; /*/ / 将Xml字符串转换成DataTable对象 / / Xml字符串 / DataTable对象 public static DataTable CXmlToDatatTable(string xmlStr) return CXmlToDataSet(xmlStr).Tables0; /*/ / 读取Xml文件信息,并转换成DataSet对象 / / / DataSet ds = new DataSet(); / ds = CXmlFileToDataSet(/XML/upload.xml); / / Xml文件地址 / DataSet对象 public static DataSet CXmlFileToDataSet(string xmlFilePath) if (!string.IsNullOrEmpty(xmlFilePath) string path = HttpContext.Current.Server.MapPath(xmlFilePath); StringReader StrStream = null; XmlTextReader Xmlrdr = null; try XmlDocument xmldoc = new XmlDocument(); /根据地址加载Xml文件 xmldoc.Load(path);DataSet ds = new DataSet(); /读取文件中的字符流 StrStream = new StringReader(xmldoc.InnerXml); /获取StrStream中的数据 Xmlrdr = new XmlTextReader(StrStream); /ds获取Xmlrdr中的数据 ds.ReadXml(Xmlrdr); return ds; catch (Exception e) throw e; finally /释放资源 if (Xmlrdr != null) Xmlrdr.Close(); StrStream.Close(); StrStream.Dispose(); else return null; /*/ / 读取Xml文件信息,并转换成DataTable对象 / / xml文江路径 / Table索引 / DataTable对象 public static DataTable CXmlToDataTable(string xmlFilePath, int tableIndex) return CXmlFileToDataSet(xmlFilePath).TablestableIndex; /*/ / 读取Xml文件信息,并转换成DataTable对象 / / xml文江路径 / DataTable对象 public static DataTable CXmlToDataTable(string xmlFilePath) return CXmlFileToDataSet(xmlFilePath).Tables0; using System; using System.Data; using System.IO; using System.Xml; using System.Text; / 相应C#代码: private string ConvertDataTableToXML(DataTable xmlDS) MemoryStream stream = null; XmlTextWriter writer = null; try stream = new MemoryStream(); writer = new XmlTextWriter(stream, Encoding.Default); xmlDS.WriteXml(writer); int count = (int)stream.Length; byte arr = new bytecount; stream.Seek(0, SeekOrigin.Begin); stream.Read(arr, 0, count); UTF8Encoding utf = new UTF8Encoding(); return utf.GetString(arr).Trim(); catch return String.Empty; finally if (writer != null) writer.Close(); private DataSet ConvertXMLToDataSet(string xmlData) StringReader stream = null; XmlTextReader reader = null; try DataSet xmlDS = new DataSet(); stream = new StringReader(xmlData); reader = new XmlTextReader(stream); xmlDS.ReadXml(reader); return xmlDS; catch (Exception ex) string strTest = ex.Message; return null; finally if (reader != null) reader.Close(); 把数据库中表的内容转存为XML文件SqlConnection conn = new SqlConnection(server=.;database=db;uid=sa;pwd=);SqlCommand cmd = conn.CreateCommand();/把列的内容作为属性,根元素名子为cmd.CommandText = select * from student for xml auto,root(root); conn.Open();XmlReader xr = cmd.ExecuteXmlReader();XmlDocument dd = new XmlDocument();dd.Load(xr); ;XmlNode xx = dd.FirstChild; /取得根元素XmlDeclaration xd = dd.CreateXmlDeclaration(1.0, utf-8, yes); /创建XML声明dd.InsertBefore(xd,xx); /在根元素前加入XML声明dd.Save(Server.MapPath(tt.config);conn.Close();执行结果:如果把上面的SqlCommand对象的CommandText改为如下:cmd.CommandText = select * from student for xml auto,root(root),elements;执行结果如下: 101 李军 男 1976-02-20T00:00:00 95033 103 陆君 男 1974-06-03T00:00:00 95031-XML与DataTable/DataSet互转(C#)2010年12月23日 评论 发表评论 /*/ 把DataSet、DataTable、DataView格式转换成XML字符串、XML文件/ public class DataToXml/*/ 将DataTable对象转换成XML字符串/ / DataTable对象 / XML字符串public static string CDataToXml(DataTable dt)if (dt != null)MemoryStream ms = null;XmlTextWriter XmlWt = null;tryms = new MemoryStream();/根据ms实例化XmlWtXmlWt = new XmlTextWriter(ms, Encoding.Unicode);/获取ds中的数据dt.WriteXml(XmlWt);int count = (int)ms.Length;byte temp = new bytecount;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();elsereturn “”;/*/ 将DataSet对象中指定的Table转换成XML字符串/ / DataSet对象 / DataSet对象中的Table索引 / XML字符串public static string CDataToXml(DataSet ds, int tableIndex)if (tableIndex != -1)return CDataToXml(ds.TablestableIndex);elsereturn CDataToXml(ds.Tables0);/*/ 将DataSet对象转换成XML字符串/ / DataSet对象 / XML字符串public static string CDataToXml(DataSet ds)return CDataToXml(ds, -1);/*/ 将DataView对象转换成XML字符串/ / DataView对象 / XML字符串public static string CDataToXml(DataView dv)return CDataToXml(dv.Table);/*/ 将DataSet对象数据保存为XML文件/ / DataSet / XML文件路径 / bool值public static bool CDataToXmlFile(DataTable dt, string xmlFilePath)if (dt != null) & (!string.IsNullOrEmpty(xmlFilePath)string path = HttpContext.Current.Server.MapPath(xmlFilePath);MemoryStream ms = null;XmlTextWriter XmlWt = null;tryms = new MemoryStream();/根据ms实例化XmlWtXmlWt = new XmlTextWriter(ms, Encoding.Unicode);/获取ds中的数据dt.WriteXml(XmlWt);int count = (int)ms.Length;byte temp = new bytecount;ms.Seek(0, SeekOrigin.Begin);ms.Read(temp, 0, count);/返回Unicode编码的文本UnicodeEncoding ucode = new UnicodeEncoding();/写文件StreamWriter sw = new StreamWriter(path);sw.WriteLine(“”);sw.WriteLine(ucode.GetString(temp).Trim();sw.Close();return true;catch (System.Exception ex)throw ex;finally/释放资源if (XmlWt != null)XmlWt.Close();ms.Close();ms.Dispose();elsereturn false;/*/ 将DataSet对象中指定的Table转换成XML文件/ / DataSet对象 / DataSet对象中的Table索引 / xml文件路径 / bool值public static bool CDataToXmlFile(DataSet ds, int tableIndex, string xmlFilePath)if (tableIndex != -1)return CDataToXmlFile(ds.TablestableIndex, xmlFilePath);elsereturn CDataToXmlFile(ds.Tables0, xmlFilePath);/*/ 将DataSet对象转换成XML文件/ / DataSet对象 / xml文件路径 / bool值public static bool CDataToXmlFile(DataSet ds, string xmlFilePath)return CDataToXmlFile(ds, -1, xmlFilePath);/*/ 将DataView对象转换成XML文件/ / DataView对象 / xml文件路径 / bool值public static bool CDataToXmlFile(DataView dv, string xmlFilePath)return CDataToXmlFile(dv.Table, xmlFilePath);/*/ XML形式的字符串、XML文江转换成DataSet、DataTable格式/ public class XmlToData/*/ 将Xml内容字符串转换成DataSet对象/ / Xml内容字符串 / DataSet对象public static DataSet CXmlToDataSet(string xmlStr)if (!string.IsNullOrEmpty(xmlStr)StringReader
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 留学生招生代理协议书详细版中文5篇
- 宝鸡网约车人证考试题库及答案
- 采油工技师实际考试题目及答案
- 初级餐饮食品安全员证考试及答案
- 行政法相关题目:行政诉讼管辖、复议赔偿等测试题附答案
- 2025年病历管理制度与病历书写规范考试题(附答案)
- 2025年电厂外包人员试题及答案
- 环境质量精准监测-洞察与解读
- 商业综合体生态环境融合-洞察与解读
- 2025年事业单位招聘考试综合类职业能力倾向测验真题模拟试卷(成都)
- 监理管理交底
- JGJT46-2024《施工现场临时用电安全技术标准》条文解读
- 锑矿合同范本
- 中医适宜技术操作规程技术操作规范
- 新入团团课培训
- 学校实验室危险化学品安全工作检查记录表
- 2024年度云南省高校教师资格证之高等教育心理学真题练习试卷A卷附答案
- 鼻饲法定义目的将胃管经一侧鼻腔插入胃内从管内灌注流质饮
- 2024年广东省清远市清城区事业单位招聘23人历年高频难、易错点500题模拟试题附带答案详解
- 医疗质量医疗安全十八项核心制度培训模板
- 2023年山西省普通高中学业水平考试真题物理试题(含答案解析)
评论
0/150
提交评论