第一种:使用XmlReader来读取。

  

 Stopwatch sw = Stopwatch.StartNew();
List<Dictionary<string, string>> entityInfo = new List<Dictionary<string, string>>();
using (XmlReader reader = new XmlTextReader(compareXmlName))
{
Dictionary<string, string> xmlValue = null;
string key = string.Empty;
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
if (string.Compare(reader.LocalName, "MultiTable",
StringComparison.OrdinalIgnoreCase) == )
{
xmlValue = new Dictionary<string, string>();
}
else if(string.Compare(reader.LocalName,"NewDataSet",
StringComparison.OrdinalIgnoreCase) != )
{
key = reader.LocalName;
}
break;
case XmlNodeType.EndElement:
if (string.Compare(reader.LocalName, "MultiTable",
StringComparison.OrdinalIgnoreCase) == )
{
if (xmlValue != null)
{
entityInfo.Add(xmlValue);
xmlValue = null;
}
}
break;
case XmlNodeType.Text:
if (xmlValue != null)
{
xmlValue.Add(key, reader.Value);
}
break;
}
}
}
TimeSpan ts = sw.Elapsed; List<Customers> packData = new List<Customers>();
foreach (var item in entityInfo)
{
Customers temp = new Customers();
foreach (var sub in item)
{
if (sub.Key == "CustomerID")
{
temp.CustomerID = int.Parse(sub.Value);
}
else if (sub.Key == "CustomerName")
{
temp.CustomerName = sub.Value;
}
else if (sub.Key == "Description")
{
temp.Description = sub.Value;
}
else if (sub.Key == "Boolean")
{
temp.IsEnabled = sub.Value == "true" ? true : false;
}
}
packData.Add(temp);
} dataGridView3.DataSource = packData;
this.textBox9.Text = ts.TotalMilliseconds.ToString();

这种方式其实是最原始的读取方法,读取的时候一个一个的节点来读取,节点的类型有很多,比如Element类型,EndElement类型,Text类型等等,通过这种方式,我们来读取我们需要的数据。这种方式效率最高。

第二种:使用XmlDocument的方式来读取:

 Stopwatch sw = Stopwatch.StartNew();
XmlDocument doc = new XmlDocument();
doc.Load(compareXmlName);
XmlNode root = doc.DocumentElement;
XmlNode child = root.FirstChild;
List<Customers> results = new List<Customers>();
while (child != null)
{
XmlNode grandSon = child.FirstChild;
Customers temp = new Customers();
while (grandSon != null)
{
if (grandSon.Name == "CustomerID")
{
temp.CustomerID = int.Parse(grandSon.FirstChild.Value);
}
else if (grandSon.Name == "CustomerName")
{
temp.CustomerName = grandSon.FirstChild.Value;
}
else if (grandSon.Name == "Description")
{
temp.Description = grandSon.FirstChild.Value;
}
else if (grandSon.Name == "Boolean")
{
temp.IsEnabled = grandSon.FirstChild.Value == "true" ? true : false;
}
grandSon = grandSon.NextSibling;
}
results.Add(temp);
child = child.NextSibling;
}
TimeSpan ts = sw.Elapsed; dataGridView3.DataSource = results;
textBox10.Text = ts.TotalMilliseconds.ToString();

这种方式其实也是通过xmlLoader来构造节点的,xmlLoader里面也是XmlReader完成的任务,这种方式的效率也比较高。

第三种:直接使用DataSet来读取,自动判断Schema信息。

           DataSet compareDataSet1 = new DataSet("CompareDataSet");
Stopwatch sw = Stopwatch.StartNew();
//读取架构信息 compareDataSet1.ReadXml(compareXmlName, XmlReadMode.Auto); TimeSpan ts = sw.Elapsed;
this.dataGridView3.DataSource = compareDataSet1.Tables[];
textBox7.Text = ts.TotalMilliseconds.ToString();

  这种方式时效率最不高的,在调用ReadXml的时候,也是通过XmlReader来完成的工作,调用XmlLoader来加载数据,加载数据是通过XmlLoader的LoadTable来构造表的数据的。

第四种:也是使用DataSet来读取数据,但是先读取架构信息,并且调用BeginLoadData来关闭通知,索引维护等,这样的速度比第三种要快。

 DataSet compareDataSet1 = new DataSet("CompareDataSet");
Stopwatch sw = Stopwatch.StartNew();
//读取架构信息
compareDataSet1.ReadXmlSchema(compareXmlSchemaName);
//防止在修改数据的过程中引发各种约束的检查,并可以提高速度
foreach (DataTable dt in compareDataSet.Tables)
{
dt.BeginLoadData();
}
compareDataSet1.ReadXml(compareXmlName, XmlReadMode.IgnoreSchema);
foreach (DataTable dt in compareDataSet1.Tables)
{
dt.EndLoadData();
}
TimeSpan ts = sw.Elapsed;
this.dataGridView3.DataSource = compareDataSet1.Tables[];
textBox6.Text = ts.TotalMilliseconds.ToString();

第五种:也还是使用DataSet来读取数据,也读取架构信息,但是不调用BeginLoadData方法,在这里,因为没有添加任何的索引,约束等,所以速度和第四种差不多。

  DataSet compareDataSet1 = new DataSet("CompareDataSet");
Stopwatch sw = Stopwatch.StartNew();
//读取架构信息
compareDataSet1.ReadXmlSchema(compareXmlSchemaName); compareDataSet1.ReadXml(compareXmlName, XmlReadMode.IgnoreSchema); TimeSpan ts = sw.Elapsed;
this.dataGridView3.DataSource = compareDataSet1.Tables[];
textBox8.Text = ts.TotalMilliseconds.ToString();
 

五种的比较结果,读取10万条数据的结果如下:

http://files.cnblogs.com/files/monkeyZhong/DataSetXML.zip

读取XML文件的几种方式的效率分析的更多相关文章

  1. java读取XML文件的四种方式

    java读取XML文件的四种方式 Xml代码 <?xml version="1.0" encoding="GB2312"?> <RESULT& ...

  2. 解析Xml文件的三种方式及其特点

    解析Xml文件的三种方式 1.Sax解析(simple api  for xml) 使用流式处理的方式,它并不记录所读内容的相关信息.它是一种以事件为驱动的XML API,解析速度快,占用内存少.使用 ...

  3. 浅谈JS中的!=、== 、!==、===的用法和区别 JS中Null与Undefined的区别 读取XML文件 获取路径的方式 C#中Cookie,Session,Application的用法与区别? c#反射 抽象工厂

    浅谈JS中的!=.== .!==.===的用法和区别   var num = 1;     var str = '1';     var test = 1;     test == num  //tr ...

  4. Java 读取 .properties 文件的几种方式

    Java 读取 .properties 配置文件的几种方式   Java 开发中,需要将一些易变的配置参数放置再 XML 配置文件或者 properties 配置文件中.然而 XML 配置文件需要通过 ...

  5. 解析xml文件的四种方式

    什么是 XML? XML 指可扩展标记语言(EXtensible Markup Language) XML 是一种标记语言,很类似 HTML XML 的设计宗旨是传输数据,而非显示数据 XML 标签没 ...

  6. Android-----解析xml文件的三种方式

    SAX解析方法介绍: SAX(Simple API for XML)是一个解析速度快并且占用内存少的XML解析器,非常适合用于Android等移动设备.SAX解析XML文件采用的是事件驱动,也就是说, ...

  7. 【开发笔记】- Java读取properties文件的五种方式

    原文地址:https://www.cnblogs.com/hafiz/p/5876243.html 一.背景 最近,在项目开发的过程中,遇到需要在properties文件中定义一些自定义的变量,以供j ...

  8. Spring 读取XML配置文件的两种方式

    import org.springframework.context.ApplicationContext; import org.springframework.context.support.Cl ...

  9. 解析XML文件的几种方式及其比较

    解析xml文件目前比较流行的主要有四种方式: 1. DOM(Document Object Model)它把整个XML文档当成一个对象加载到内  存,不管文档有多大.它一般处理小文件 2.SAX(Si ...

随机推荐

  1. [异常解决] Make nRF51 DFU Project Appear "fatal error: uECC.h: No such file or directory"

    What's the problem When I make the nRF51's DFU project appear "no uECC.h" error: And then ...

  2. iOS开发网络篇-JSON文件的解析

    一.什么是JSON数据 1.JSON的简单介绍 JSON:是一种轻量级的传输数据的格式,用于数据的交互 JSON是javascript语言的一个子集.javascript是个脚本语言(不需要编译),用 ...

  3. svn , github工作流

    svn 需要有一台中央服务器,所有的分支,主干,标签,全都保存在这台中央服务器上.开发着需要提交代码时,需要保持中央服务器连接.切换分支时会有本地与服务器网络连接. git 改进了这一点,每台安装有g ...

  4. 编写优美的GTest测试案例

    http://www.cnblogs.com/coderzh/archive/2010/01/09/beautiful-testcase.html 使用gtest也有很长一段时间了,这期间也积累了一些 ...

  5. mongoose CastError: Cast to ObjectId failed for value

    restfull路由如下: router.get('/:id', controller.show); mongoes代码如下: exports.show = function(req, res) { ...

  6. Shortest Prefixes

    poj2001:http://poj.org/problem?id=2001 题意:给你一些单词,然后让你寻找每个单词的一个前缀,这个前缀能够唯一表示这个单词,并且是最短的. 题解:直接用trie树来 ...

  7. 【博弈论】HDU 5754 Life Winner Bo

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5754 题目大意: 4种棋子,象棋中的 1王,2车,3马,4后,选其一,B和G轮流走,不能往左上走,一 ...

  8. 谈谈托管代码、IL、CLR、ISAPI?

    什么是托管代码?       托管代码是可以使用20多种支持Microsoft .NET Framework的高级语言编写的代码,这些语言包括:C#, J#, Microsoft Visual Bas ...

  9. 状压dp-poj-1170-Shopping Offers

    题目链接: http://poj.org/problem?id=1170 题目意思: 购物车里有b种(0=<b<=5)物品,每种物品告诉物品代号c(1=<c<=999),数量为 ...

  10. TortoiseGit上传代码到GitHub

    Github是管理软件开发的首选托管网站,12306的火车票插件一时让国内当时很多小白开发者(当然也包括我)认识到了这个网站.GitHub可以托管各种git库,并提供一个web界面,与 SourceF ...