XML数据读取方式性能比较(一)
几个月来,疑被SOA,一直在和XML操作打交道,SQL差不多又忘光了。现在已经知道,至少有四种常用人XML数据操作方式(好像Java差不多),不过还没有实际比较过这些方式各有哪些特点或优劣。正好看到网上也没有这方面的实验,偶来总结一下。
测试开始先读取XML源,用一个比较大的RSS文件链接,复制到项目bin/debug目录下。
Stream xmlStream =new MemoryStream(File.ReadAllBytes(path));
一、XmlDocument 方式
{
var doc =new XmlDocument();
doc.Load(xmlStream);
var nodeList = doc.DocumentElement.ChildNodes;
var lstChannel =new List<Object>(nodeList.Count );
foreach (XmlNode node in nodeList)
{
var channel =new
{
Title = node.SelectSingleNode("title").InnerText,
Link = node.SelectSingleNode("link").InnerText,
Description = node.SelectSingleNode("description").InnerText,
Content = node.SelectSingleNode("content").InnerText,
PubDate = node.SelectSingleNode("pubDate").InnerText,
Author = node.SelectSingleNode("author").InnerText,
Category = node.SelectSingleNode("category").InnerText
};
lstChannel.Add(channel);
}
return lstChannel;
}
二、XPathNavigator 方式
{
var doc =new XmlDocument();
doc.Load(xmlStream);
var nav = doc.CreateNavigator();
nav.MoveToRoot();
var nodeList = nav.Select("/channel/item");
var lstChannel =new List<Object>(nodeList.Count);
foreach (XPathNavigator node in nodeList)
{
var channel =new
{
Title = node.SelectSingleNode("title").Value,
Link = node.SelectSingleNode("link").Value,
Description = node.SelectSingleNode("description").Value,
Content = node.SelectSingleNode("content").Value,
PubDate = node.SelectSingleNode("pubDate").Value,
Author = node.SelectSingleNode("author").Value,
Category = node.SelectSingleNode("category").Value
};
lstChannel.Add(channel);
}
return lstChannel;
}
三、XmlTextReader 方式
{
var lstChannel =new List<Channel>();
var reader = XmlReader.Create(xmlStream);
while (reader.Read())
{
if (reader.Name =="item"&& reader.NodeType == XmlNodeType.Element)
{
var channel =new Channel();
lstChannel.Add(channel);
while (reader.Read())
{
if (reader.Name =="item") break;
if (reader.NodeType != XmlNodeType.Element) continue;
switch (reader.Name)
{
case"title":
channel.Title = reader.ReadString();
break;
case"link":
channel.Link = reader.ReadString();
break;
case"description":
channel.Description = reader.ReadString();
break;
case"content":
channel.Content = reader.ReadString();
break;
case"pubDate":
channel.PubDate = reader.ReadString();
break;
case"author":
channel.Author = reader.ReadString();
break;
case"category":
channel.Category = reader.ReadString();
break;
default:
break;
}
}
}
}
return lstChannel;
}
四、Linq to XML 方式
{
var xd = XDocument.Load(xmlStream);
var list = from node in xd.Elements("channel").Descendants("item")
select new
{
Title = node.Element("title").Value,
Link = node.Element("link").Value,
Description = node.Element("description").Value,
Content = node.Element("content").Value,
PubDate = node.Element("pubDate").Value,
Author = node.Element("author").Value,
Category = node.Element("category").Value
};
return list.ToList();
}
测试结果:
XmlDocment | 47ms |
XPathNavigator | 42ms |
XmlTextReader | 23ms |
Xml Linq | 28ms |
小结一下自己的认识,XmlDocument的操作基本按W3C的DOM操作方式,不过要将全部节点解析成对象加载到内存中,往往造成很大浪费。所以微软自己的编程规范也不推荐用它。这里由于读取了所有节点,可能因此性能和Navigator方式相差不大。在三种随机读取方式中,Xml Linq性能最高,只是方法名有点别扭。XmlTextReader方式是所谓的SAX,只读向前,无疑性能最高,不过实现上麻烦了不少,要比较精确的控制访问逻辑,也无法用匿名类存储数据。
.Net 3.5发布Xml Linq可以很好地取代前两种方式,通常情况下,最好用它。只有个别场合,如果对性能要求极高,或者读取Xml数据量太大不能一下子下载或读取到内存中,那就只好痛苦委身于XmlTextReader了。
XML数据读取方式性能比较(一)的更多相关文章
- geotrellis使用(二)geotrellis-chatta-demo以及geotrellis框架数据读取方式初探
在上篇博客(geotrellis使用初探)中简单介绍了geotrellis-chatta-demo的大致工作流程,但是有一个重要的问题就是此demo如何调取数据进行瓦片切割分析处理等并未说明,经过几天 ...
- Android使用webService(发送xml数据的方式,不使用jar包)
Android使用webService可以用ksoap2.jar包来使用.但是我觉得代码不好理解,而且记不住. 所以我查询了好多资料,以及自己的理解.可以用代码发送http请求(发送xml数据)来访问 ...
- OleDbDataReader快速数据读取方式
查询得到OleDbDataReader后,有三种方式支持数据读取,如下: //方法一**速度中等 OleDbDataReader reader = command.ExecuteReader(); w ...
- TensorFlow数据读取方式:Dataset API
英文详细版参考:https://www.cnblogs.com/jins-note/p/10243716.html Dataset API是TensorFlow 1.3版本中引入的一个新的模块,主要服 ...
- XML教程、语法手册、数据读取方式大全
XML简单易懂教程 本文提供全流程,中文翻译.Chinar坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) 一 XML --数据格式的写法 二 Re ...
- XML数据读取——Digester简单使用
>>>>>>>>>>>>>>>>>>>>>>>>> ...
- MYSQL 的数据读取方式
例子: create table T(X bit(8)); insert into T (X) values(b'11111111'); select X from T; 这个时候会发现这个X 是乱码 ...
- TensorFlow全新的数据读取方式:Dataset API入门教程
TensorFlow.data : http://tech.ifeng.com/a/20171109/44752505_0.shtml Pytorch:https://ptorch.com/docs/ ...
- Qt读取JSON和XML数据
QJSON JSON(JavaScript Object Notation)是一个轻量级的数据交换格式; 可以将数据以name/value的形式任意组合; QJson 是一个基于Qt的库, 将JSON ...
随机推荐
- [置顶] Guava学习之Immutable集合
Immutable中文意思就是不可变.那为什么需要构建一个不可变的对象?原因有以下几点: 在并发程序中,使用Immutable既保证线程安全性,也大大增强了并发时的效率(跟并发锁方式相比).尤其当一个 ...
- Cocos2dx项目启程二 之 封装属于我的按钮类
不知道为什么,很讨厌cocos2dx的 各菜单类,比如按钮:如果一张图片上就已经有按钮的几个状态了,我还是要创建多张资源图片, 最起码要指定这张图片上哪块区域是这个普通状态,哪块区域是那个选中状态.. ...
- iOS得知1_初体验
UIView:父类的所有控件,所有的UIView它是一个容器.可容纳其他UIView UIController:用于控制UIView,责创建/销毁自己的UIView,显示/隐藏UIView.处理UIV ...
- 读书笔记:《梦断代码Dreaming in Code》
读书笔记:<梦断代码Dreaming in Code> 拿到<梦断代码>书后,一口气翻了一遍,然后又用了3天时间仔细读了一遍,也不禁掩卷长叹一声,做软件难.虽难,仍要继续走下去 ...
- [置顶] JQuery实战总结三 标签页效果图实现
在浏览网站时我们会看到当我们鼠标移到多个选项卡上时,不同的选项卡会出现自己对应的界面的要求,在同一个界面上表达了尽量多的信息.大大额提高了空间的利用率.界面的切换效果也是不错的哦,这次自己可以实现啦. ...
- 牛逼的验证码,printf返回值
牛逼的验证码,如下图, 结果是4321,为什么呢,主要是printf返回值问题?那么printf到底返回什么? 经查阅,printf的返回值是打印的字符个数,因此结果是4321就很明显了.
- C#之网络
首先很不好意思,前段时间把评论的功能给关掉啦,BUT NOW 此功能以开放,欢迎小伙伴们拍砖. 1网络 在网络环境下,我们最感兴趣的两个名称空间是System.Net和System.Net.Socke ...
- 网络安全之IP伪造
眼下非常多站点的涉及存在一些安全漏洞,黑客easy使用ip伪造.session劫持.xss攻击.session注入等手段危害站点安全.在纪录片<互联网之子>(建议搞IT的都要看下)中.亚伦 ...
- windows接口被占用
netsh winsock reset 重启winsock服务
- ecshop 后台批量上传商品 完整上传
ecshop 后台批量上传商品,之所以无法上传,是因为后台上传php文件方法中没有导入商品原图路径 将ecshop根目录中的admin/goods_batch.php文件全部修改为 <?php ...