XML VS DataSe
来源:互联网 发布:移动硬盘 知乎 编辑:程序博客网 时间:2024/05/22 02:25
系列目录
前言
通过前三篇介绍,相信大家对于XML已经有了很多的理解,后两篇将介绍XML和常用数据结构的转换。本篇我们一起讨论XML和DataSet(DataTable)之间的羁绊。
XML与DataSet的关系
DataSet是ADO.NET的中心概念。可以把DataSet当成内存中的数据库,DataSet是不依赖于数据库的独立数据集合。所谓独立,就是说,即使断开数据链路,或者关闭数据库,DataSet依然是可用的,DataSet在内部是用XML来描述数据的,由于XML是一种与平台无关、与语言无关的数据描述语言,而且可以描述复杂关系的数据,比如父子关系的数据,所以DataSet实际上可以容纳具有复杂关系的数据,而且不再依赖于数据库链路。[百度百科]
在.NET Framework 中,经常使用XML 作为存储和传输各种数据的格式。DataSet 中的数据可以转换成XML 的形式来表示和存储。我们可以使用XML 对象同步和转换DataSet 的数据,而DataSet 也可以存储和传输XML 格式的数据。XML 与 DataSet 的关系如下图所示:
不管是DataSet/DataTable 其实底层内部都是通过XML组织数据的。
DataSet类与XML关联常用方法:
1. ReadXml()2. ReadXmlSchema()3. WriteXml()4. WriteXmlSchema()5. GetXml()6. GetXmlSchema()
XmlSchema是对XML的文档结构的描述。XmlSchema教程
ReadXml/ReadXmlSchema
DataSet和XML相互转换(XmlDataSetConvert)
为了方便DS和XML转换这边整理对应的封装类 XmlDataSetConvert,类图如下:
将xml字符串转换为DataSet
#region 将xml字符串转换为DataSet/// <summary>/// 将xml对象内容字符串转换为DataSet/// </summary>/// <param name="xmlData"></param>/// <returns></returns>public static DataSet XmlString2DataSet(string xmlData){ StringReader stream = null; XmlTextReader reader = null; DataSet xmlDS = null; try { xmlDS = new DataSet(); using (stream = new StringReader(xmlData)) { //从stream装载到XmlTextReader using (reader = new XmlTextReader(stream)) { xmlDS.ReadXml(reader); } } return xmlDS; } catch (System.Exception ex) { throw ex; } finally { if (reader != null) reader.Close(); }}#endregion 将xml字符串转换为DataSet
将xml文件转换为DataSet
#region 将xml文件转换为DataSet/// <summary>/// 将xml文件转换为DataSet/// </summary>/// <param name="xmlFile">文件路径</param>/// <returns></returns>public static DataSet XmlFile2DataSet(string xmlFile){ XmlTextReader reader = null; try { DataSet xmlDS = new DataSet(); //从stream装载到XmlTextReader using (reader = new XmlTextReader(xmlFile)) { xmlDS.ReadXml(reader); } //xmlDS.ReadXml(xmlFile); return xmlDS; } catch (System.Exception ex) { throw ex; } finally { if (reader != null) reader.Close(); }}#endregion 将xml文件转换为DataSet
将DataSet转换为xml对象字符串
#region 将DataSet转换为xml对象字符串/// <summary>/// 将DataSet转换为xml对象字符串/// </summary>/// <param name="xmlDS"></param>/// <returns></returns>public static string DataSet2XmlString(DataSet xmlDS){ MemoryStream stream = null; XmlTextWriter writer = null; try { stream = new MemoryStream(); //从stream装载到XmlTextReader //writer = new XmlTextWriter(stream, Encoding.Unicode);//Unicode有点问题,可能是字符集不一致 writer = new XmlTextWriter(stream, Encoding.Default); //用WriteXml方法写入文件. xmlDS.WriteXml(writer); int count = (int)stream.Length; byte[] arr = new byte[count]; stream.Seek(0, SeekOrigin.Begin); stream.Read(arr, 0, count); //UnicodeEncoding utf = new UnicodeEncoding(); //return utf.GetString(arr).Trim(); return Encoding.Default.GetString(arr).Trim(); } catch (System.Exception ex) { throw ex; } finally { if (writer != null) writer.Close(); }}#endregion 将DataSet转换为xml对象字符串
将DataSet转换为xml对象字符串
#region 将DataSet转换为xml文件/// <summary>/// 将DataSet转换为xml文件/// </summary>/// <param name="xmlDS"></param>/// <param name="xmlFile"></param>public static void DataSet2XmlFile(DataSet xmlDS, string xmlFile){ try { xmlDS.WriteXml(xmlFile); } catch (System.Exception ex) { throw ex; } #region 复杂实现方法 //MemoryStream stream = null; //XmlTextWriter writer = null; //try //{ // stream = new MemoryStream(); // //从stream装载到XmlTextReader // //writer = new XmlTextWriter(stream, Encoding.Unicode); // writer = new XmlTextWriter(stream, Encoding.Default); // //用WriteXml方法写入文件. // xmlDS.WriteXml(writer); // int count = (int)stream.Length; // byte[] arr = new byte[count]; // stream.Seek(0, SeekOrigin.Begin); // stream.Read(arr, 0, count); // //返回Unicode编码的文本 // //UnicodeEncoding utf = new UnicodeEncoding(); // StreamWriter sw = new StreamWriter(xmlFile); // sw.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); // sw.WriteLine(Encoding.Default.GetString(arr).Trim()); // sw.Close(); //} //catch (System.Exception ex) //{ // throw ex; //} //finally //{ // if (writer != null) writer.Close(); //} #endregion 复杂实现方法}#endregion 将DataSet转换为xml文件
总结
这边主要谈DataSet和XML关系。并总结封装了他们转换的类源代码
XML 与DataSet 对象的关系
参考地址
百度百科
0 0
- XML VS DataSe
- XML VS DataSe
- XML VS DataSe
- JSON vs YAML vs XML
- Annotation VS. XML VS. interface
- PB vs XML vs JSON
- JSON vs ProtoBuf vs XML
- clientaccesspolicy.xml vs. crossdomain.xml
- XML attribute vs XML element
- XML VS. CSV
- xml db vs. RDB
- JSON VS XML
- JSON VS XML
- XML(e4x) vs AMF
- JSON vs XML
- SOAP vs XML-RPC
- VS “XML 文档” 说明
- XML 属性vs元素
- SVN命令解析以及问题解决(update...)
- opencv之Mat数据类型和ImlImage数据类型以及CvMat数据类型得转换
- 【搜索-广搜】poj3984 迷宫问题
- linux命令
- ODI主键报错--create unique index on flow table
- XML VS DataSe
- HTTPS原理解析
- 复杂的MySQL 时间查询语句解析
- JAVA高级【3.3】《Java核心技术2》网络-使用 URL 读取网络资源
- Java反射机制详解
- 初始Hibernate框架
- java基础学习面向对象之final关键字 五-9
- Java-Base64加密&&MD5加密
- View和ViewGroup 的measure过程