简体   繁体   中英

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. I need to find a way to get as quickly as possible ...

My XML:

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

I want to get chNFe node's value

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?

Searching for the descendant node via XName will be slightly faster:

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.

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:

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.

My test results (1,000,000 runs of each, average time):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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