Bruno Henri
Bruno Henri

Reputation: 373

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?

Upvotes: 1

Views: 194

Answers (2)

StriplingWarrior
StriplingWarrior

Reputation: 156624

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:

enter image description here

Upvotes: 2

Cᴏʀʏ
Cᴏʀʏ

Reputation: 107566

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

Upvotes: 0

Related Questions