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

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
原创粉丝点击