How to Find XML element by name with XElement in LINQ
By FoxLearn 3/1/2025 4:03:18 AM 2
There are two main methods you can use for this task:
XElement.Descendants(name)
: Recursively searches for all descendants with a specific name.XElement.Elements(name)
: Searches only the immediate child elements for those with the given name. This is useful when you want to limit the search to just one level down.
Both methods return matching elements as an IEnumerable<XElement>
. You can then use LINQ methods (or a foreach
loop) to manipulate or display these elements.
Finding Elements Using Descendants()
Here’s an example where we use XElement.Descendants()
to find all elements named Movie
:
using System.Linq; using System.Xml.Linq; var xmlString = @" <Movies> <Movie>Shawshank Redemption</Movie> <Movie>The Matrix</Movie> <Movie>Inception</Movie> </Movies>"; var moviesXml = XElement.Parse(xmlString); var movieCount = moviesXml.Descendants("Movie").Count(); Console.WriteLine($"There are {movieCount} movies");
Ouput:
There are 3 movies
Retrieving Element Values
To extract values from the elements, you can use the XElement.Value
property, which gives you the content of an element as a string.
Here’s an example where we extract and display the names of all Title
elements:
using System.Linq; using System.Xml.Linq; var xmlString = @" <Movies> <Movie> <Title>Shawshank Redemption</Title> <Year>1994</Year> </Movie> <Movie> <Title>The Matrix</Title> <Year>1999</Year> </Movie> <Movie> <Title>Inception</Title> <Year>2010</Year> </Movie> </Movies>"; var moviesXml = XElement.Parse(xmlString); foreach (var titleElement in moviesXml.Descendants("Title")) { Console.WriteLine(titleElement.Value); }
Output:
Shawshank Redemption The Matrix Inception
Notice that XElement.Descendants()
searches all descendants recursively, so it finds Title
elements at any level in the hierarchy.
Modifying Element Values
After finding the elements, you can modify their values in two ways:
- Change the element’s string value directly using
XElement.Value
. - Use
XElement.SetValue()
to set a value that is not a string. It will automatically convert the object to a string.
Here’s an example where we change the values of both the Title
and Streamable
elements for each Movie
:
using System.Linq; using System.Xml.Linq; var xmlString = @" <Movies> <Movie> <Title>Shawshank Redemption</Title> <Streamable>false</Streamable> </Movie> <Movie> <Title>The Matrix</Title> <Streamable>false</Streamable> </Movie> </Movies>"; var moviesXml = XElement.Parse(xmlString); foreach (var movie in moviesXml.Descendants("Movie")) { // Change the Title to uppercase movie.Element("Title").Value = movie.Element("Title").Value.ToUpper(); // Set Streamable to true movie.Element("Streamable").SetValue(true); } Console.WriteLine(moviesXml.ToString());
Output:
<Movies> <Movie> <Title>SHAWSHANK REDEMPTION</Title> <Streamable>true</Streamable> </Movie> <Movie> <Title>THE MATRIX</Title> <Streamable>true</Streamable> </Movie> </Movies>
In this example, we’ve modified the Title
element values by converting them to uppercase and changed the Streamable
element value to true
.
By using XElement.Descendants()
and XElement.Elements()
, you can efficiently navigate, search, and modify XML structures in C# with LINQ-to-XML.
- Could not find an implementation of the query pattern for source type
- Filtering Collections in LINQ
- Element Operators in LINQ
- Inner Join Using LINQ And Lambda
- Understanding Single, SingleOrDefault, First, and FirstOrDefault in LINQ
- Creating Dynamic LINQ Queries in C# with Predicate Builder
- How to Use Select and SelectMany Effectively in LINQ
- Optimizing LINQ: Best Practices and Tips