如何以最快的方式从Xml中获取节点值? [英] How to fetch the node values from an Xml in the fastest manner ?
本文介绍了如何以最快的方式从Xml中获取节点值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何以最快的方式从Xml中获取节点值?
我想以最快的方式获取节点。我已经尝试使用Xml节点列表,然后使用foreach和validate从相同的循环中获取XmlNode。这很耗时。
How to fetch the node values from an Xml in the fastest manner ?
I want to fetch nodes in the most fast manner. I have tried using Xml Node List and then fetch the XmlNode from the same and loop using foreach and validate. It is time consuming.
推荐答案
最有可能的是,最快的将使用类System.Xml.XmlReader
:
https:/ /msdn.microsoft.com/en-us/library/system.xml.xmlreader%28v=vs.110%29.aspx [ ^ ]
只有当您的XML非常小时,更快的方法可能是在其中进行一些 ad-hoc 字符串搜索,但我建议通过各种方式避免这些事情,原因很明显。
-SA
Most likely, the fastest will be using the classSystem.Xml.XmlReader
:
https://msdn.microsoft.com/en-us/library/system.xml.xmlreader%28v=vs.110%29.aspx[^]
Only if your XML is extremely small, the faster method could be some ad-hoc string search in it, but I would recommend to avoid such things by all means, by pretty obvious reasons.
—SA
As Sergey Aleksandrovich Kryukov [ ^ ]提到它取决于文件大小。 对于小部分数据,即使您使用 Linq to xml [ ^ ]。
想象一下,xml结构如下:
As Sergey Aleksandrovich Kryukov[^] mentioned it depends on file size. For small portion of data the difference measured in time of reading xml file is unnoticeable, even if you use Linq to xml[^].
Imagine, an xml structure looks like:
Root
--Level1
|--Level2
|--Level3
|--Level4
|--Level5
|--Level6
|--Level7
|--Level8
您希望从节点获取节点(数据) Level8
,所以你可以使用这样的查询实现:
You want to get nodes (data) from node at Level8
, so you can achieve that using query like this:
XDocument xdoc = XDocument.Load("fullfilename.xml");
var result = xdoc.Descendants("Level8")
.Descendants()
.Select(a=>a);
如需了解更多信息,请参阅:
LINQ to XML [ ^ ]
LINQ to XML Overview [ ^ ]
基本查询(LINQ to XML) [ ^ ]
For further information, please see:
LINQ to XML[^]
LINQ to XML Overview[^]
Basic Queries (LINQ to XML)[^]
node.SelectNodes(some some some here);
这是选择节点而没有循环概念的最快方法。
node.SelectNodes("some XPath here");
This is fastest way to select node without looping concept.
这篇关于如何以最快的方式从Xml中获取节点值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文