Florian
Florian

Reputation: 4728

Linq to XML : Increase performance

I want to read a xml file using Linq. This xml file is composed of 1 header and N Sub-Elements like this :

<rootNode>
         <header>
            <company name="Dexter Corp." />
         </header>
         <elements>
           <element>one</element>
           <element>eleven</element>
           <element>three</element>
           <element>four</element>
           <element>five</element>
           <element>three</element>
           <element>two</element>
           <element>two</element>
         </elements>
</rootNode>

I want to retrieve elements which are a value (example : two). And only if I retrieve elements, I get the header elements.

Today, I do like this :

        string xmlFilePath = @"C:\numbers.xml";
        XDocument doc = XDocument.Load(xmlFilePath);

        var header = doc.Descendants().Elements("header");
        var elements = doc.Descendants()
            .Elements("elements")
                .Elements("element")
                .Where(el => el.Value == "two");

        // I get this values even if there is no 'elements'
        string companyName = header.Descendants("company").Attributes("name").Single().Value;
        string serialNumber = header.Descendants("serial").Single().Value;


        return elements.Select(el => new {Company = companyName, Serial = serialNumber, Value = el.Value});

Is there a better way to parse the file ? (and increase performance ?)

Upvotes: 1

Views: 1008

Answers (1)

svick
svick

Reputation: 244777

If performance is important to you, you shouldn't use doc.Descendants() to look for an element at some known location. It always scans the whole document, which is slow if the document is big.

Instead, do something like

doc.Root.Element("header")

or

doc.Root.Element("elements")
        .Elements("element")
        .Where(el => el.Value == "two")

That should be much faster.

Upvotes: 5

Related Questions