Edward Tanguay
Edward Tanguay

Reputation: 193302

How can I get the first element after an element with LINQ-to-XML?

With this code I can get the title out of the following XML file:

var xml = XElement.Load (@"C:\\test\\smartForm-customersMain.xml");
string title = xml.Element("title").Value;

But how do I make it more exact, e.g. "get the first element after the smartForm element, e.g. something like this:

//PSEUDO-CODE:
string title = xml.Element("smartForm").FirstChild("title");

The XML:

<?xml version="1.0" encoding="utf-8" ?>
<smartForm idCode="customersMain">
    <title>Customers Main222</title>
    <description>Generic customer form.</description>
    <area idCode="generalData" title="General Data">
        <column>
            <group>
                <field idCode="anrede">
                    <label>Anrede</label>
                </field>
                <field idCode="firstName">
                    <label>First Name</label>
                </field>
                <field idCode="lastName">
                    <label>Last Name</label>
                </field>
            </group>
        </column>
    </area>
    <area idCode="address" title="Address">
        <column>
            <group>
                <field idCode="street">
                    <label>Street</label>
                </field>
                <field idCode="location">
                    <label>Location</label>
                </field>
                <field idCode="zipCode">
                    <label>Zip Code</label>
                </field>
            </group>
        </column>
    </area>
</smartForm>

Upvotes: 7

Views: 20249

Answers (3)

Alexey Obukhov
Alexey Obukhov

Reputation: 854

My task was to find first child of specified name. If xml uses namespaces, than instead of

e.Elements(name).FirstOrDefault()

Write

e.Elements().FirstOrDefault(i => i.Name.LocalName == name)

Upvotes: 0

ShuggyCoUk
ShuggyCoUk

Reputation: 36438

To add slightly to Andrew's answer if you do not know whether smartForm is the root element but still want the title text of the first such entry you would use:

xml.DescendantsAndSelf("smartForm").Descendants("title").First().Value;

This requires that there be a smartForm element with a title element somewhere within it.

If you wanted to ensure that the title element was an immediate child in smartForm you could use:

xml.DescendantsAndSelf("smartForm").Elements("title").First().Value;

If you didn't care what the name of title was and just wanted the first sub element then you would use:

xml.DescendantsAndSelf("smartForm").Elements().First().Value;

Upvotes: 6

Andrew Hare
Andrew Hare

Reputation: 351516

You want to use the Descendants axis method and then call the FirstOrDefault extension method to get the first element.

Here is a simple example:

using System;
using System.Linq;
using System.Xml.Linq;

class Program
{
    static void Main()
    {
        String xml = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
            <smartForm idCode=""customersMain"">
                <title>Customers Main222</title>
                <description>Generic customer form.</description>
                <area idCode=""generalData"" title=""General Data"">
                <column>
                    <group>
                    <field idCode=""anrede"">
                        <label>Anrede</label>
                    </field>
                    <field idCode=""firstName"">
                        <label>First Name</label>
                    </field>
                    <field idCode=""lastName"">
                        <label>Last Name</label>
                    </field>
                    </group>
                </column>
                </area>
                <area idCode=""address"" title=""Address"">
                <column>
                    <group>
                    <field idCode=""street"">
                        <label>Street</label>
                    </field>
                    <field idCode=""location"">
                        <label>Location</label>
                    </field>
                    <field idCode=""zipCode"">
                        <label>Zip Code</label>
                    </field>
                    </group>
                </column>
                </area>
            </smartForm>";

        XElement element = XElement.Parse(xml)
            .Descendants()
            .FirstOrDefault();
    }
}

Upvotes: 4

Related Questions