Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 926
Edward Tanguay Avatar asked Aug 03 '09 13:08

Edward Tanguay


People also ask

How to find element in XML c#?

Search elements by attribute First, you can fetch an element's attribute by using XElement. Attribute(name). Then you can look at its value by using the . Value property.

How to get XElement attribute value in c#?

If you are getting the value and the attribute might not exist, it is more convenient to use the explicit conversion operators, and assign the attribute to a nullable type such as string or Nullable<T> of Int32 . If the attribute does not exist, then the nullable type is set to null.

What is the difference between XDocument and XmlDocument?

XDocument is from the LINQ to XML API, and XmlDocument is the standard DOM-style API for XML. If you know DOM well, and don't want to learn LINQ to XML, go with XmlDocument . If you're new to both, check out this page that compares the two, and pick which one you like the looks of better.


1 Answers

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;
like image 157
ShuggyCoUk Avatar answered Nov 10 '22 14:11

ShuggyCoUk