简体   繁体   English

有没有使用LINQ读取XML字段的更有效方法?

[英]Is there a more efficient method of reading XML fields with LINQ?

In the future I will read 200,000 xml files and from each file capture some information. 将来,我将读取200,000个xml文件,并从每个文件中捕获一些信息。 I need to find a way to get as quickly as possible ... 我需要找到一种方法来尽快获得...

My XML: 我的XML:

<note>
    <fields>
       <name>john</name>
       <lastname>doe</lastname>
    </fields>
    <info>
        <chNFe>VALUE</chNFe>
    </info>
</note>

I want to get chNFe node's value 我想获取chNFe节点的值

string xml = File.ReadAllText(@"C:\myxml.xml");
Regex.Replace(xml, @"[^\u0000-\u007F]", string.Empty);

var doc = XDocument.Parse(xml);
var matchingElements = doc.Descendants().Where(x => x.Name.LocalName == "chNFe");
string chave = matchingElements.First().Value;

Console.WriteLine("Chave: " + chave);

Is there a more efficient method of reading XML fields with LINQ? 有没有使用LINQ读取XML字段的更有效方法?

Searching for the descendant node via XName will be slightly faster: 通过XName搜索后代节点会稍微快一些:

var chave = doc.Descendants("chNFe").First().Value;

Update : Grabbing elements directly is a tiny bit faster still: 更新 :直接抓取元素仍然要快一点:

var chave = doc.Root.Element("info").Element("chNFe").Value;

However, the vast majority of the time your program spends will be in reading from the disk and parsing the XML documents, so you're probably not going to get noticeable gains as long as you're using LINQ to XML. 但是,程序花费的大部分时间都将用于从磁盘读取和解析XML文档,因此,只要使用LINQ to XML,就可能不会获得明显的收益。

Here's my benchmark code. 这是我的基准代码。 And here are the results: 结果如下:

在此处输入图片说明

What you have is pretty darn fast, but drilling down through the tree explicitly seems to be even faster. 您拥有的东西相当快,但是显式地深入树中似乎更快。

var doc = XDocument.Parse(xml);
var chave = doc.Root.Element("info").Element("chNFe").Value;

using an XPath query might be a way to maintain about the same performance but simplify your code: 使用XPath查询可能是保持大致相同的性能但简化代码的一种方式:

var doc = XDocument.Parse(xml);
var chave = doc.XPathSelectElement("/note/info/chNFe").Value;

Also, you probably don't have to read the file content separately from parsing it; 同样,您可能不必将文件内容与分析文件分开读取; use XDocument.Load to supply a path to a file and let it do the reading. 使用XDocument.Load提供文件的路径并让它进行读取。

My test results (1,000,000 runs of each, average time): 我的测试结果(每次运行1,000,000,平均时间):

1. LINQ -> Descendants() = 0.000019ms
2. XPath                 = 0.000024ms
3. LINQ -> Element()     = 0.000004ms

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM