解析Xml的最有效方法 [英] The most efficient way to parse Xml

查看:87
本文介绍了解析Xml的最有效方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.Net框架现在(至少)具有四种不同的读取Xml字符串的方法.我已经使用了XmlDocument,XmlReader,XPath和XElement中的每一个,但是在编码或执行过程中使用哪种最有效?每个设计用于不同的任务,优点和缺点是什么?

The .Net framework now has (at least) four different methods of reading an Xml string. I've used each of XmlDocument, XmlReader, XPath and XElement, but which is the most efficient to use when coding or during execution? Is each designed for a different task, what are the pros and cons?

更新: 使用XmlReader似乎是读取xml的最快方法,这对我来说似乎很合理,但有其局限性.我想知道XmlDocument和XLinq在非顺序访问xml之间是否存在任何性能差异.

Update: Using a XmlReader appears to be the quickest way to read xml, which sound reasonable to me, but has it's limitations. I would like to know if there is any performance difference between XmlDocument and XLinq for accessing xml non-sequentially.

更新: 我发现一些文章比较了加载xml文档的不同方法. XmlReader是最快的,XmlDocument和LINQ to XML之间没有明显的区别,直到您加载具有10,000+节点的文档为止,其中LINQ to XML排在了前面.

Update: I found some posts comparing the different methods of loading an xml document. XmlReader is the fastest, there is insignificant difference between XmlDocument and LINQ to XML until you load a document with 10,000+ node where LINQ to XML comes out in front.

  • http://www.nearinfinity.com/blogs/page/jferner?entry=performance_linq_to_sql_vs
  • http://www.hanselman.com/blog/AtAGlanceXmlReaderVsXPathNavigatorVsXmlDocument.aspx

推荐答案

三种最常见的读取方法是:

The three most common methods to read are:

XmlDocument 它以树结构读取整个文件,然后可以使用XPath或浏览所有节点来访问该文件.由于必须将整个XML结构加载到内存中,因此对于非常大的文件,它需要大量内存.对于较小的文件(少于几兆),非常好且易于使用.

XmlDocument It reads the whole file in a tree structure that can then be accessed using XPath or by browsing all the nodes. It requires a lot of memory for very large file since the whole XML structure must be loaded in memory. Very good and simple to use for smaller files (less then a few megs).

XmlReader 速度很快,但是使用起来又很麻烦,因为它是顺序的.如果您需要返回,则不必这样做,XML结构通常很容易出现混乱的订单.另外,如果您从不间断的XML流中读取数据,那么这可能是唯一的方法.

XmlReader Fast, but also a real pain to use since it's sequential. If you ever need to go back, you can't, and XML structure are usually very prone to having disorganised orders. Also, if you read from a non ending stream of XML, this is probably the only way to go.

XML序列化器 这基本上为您完成了所有工作,您提供了模型的根对象,并为您创建和读取了XML.但是,您几乎无法控制结构,读取对象的旧版本是一件很痛苦的事情.因此,这对于持久性而言效果不佳.

XML serializers This basically does everything for you, you provide the root object of your model and it creates and read the XML for you. However, you have almost no control over the structure, and reading older versions of your object is a pain. So this won't work very well for persistance.

XDocument和LINQ to XML 正如Daniel Straight指出的那样.但是我还不足以发表评论.我邀请任何人编辑帖子并添加缺少的信息.

XDocument and LINQ to XML As Daniel Straight pointed out. But I don't know it enough to comment. I invite anyone to edit the post and add the missing info.

现在写作是另一个故事.维护XmlDocument很痛苦,而使用XmlWriter却轻而易举.

Now writing is another story. It's a pain to maintain a XmlDocument and XmlWriter is a breeze to use.

根据我的经验,我认为最好的组合是使用XmlWriter编写并使用XmlDocument读取.

I'd say, from my experience, that the best combo is to write using XmlWriter and read using XmlDocument.

这篇关于解析Xml的最有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆