C# – Find XML element by name with XElement (Linq)

Use the XElement class (from the Linq-to-Xml API) to search for XML elements by name. There are two main methods you can use to do this:

  • XElement.Descendants(name): Recursively searches all descendants for elements with name.
  • XElement.Elements(name): Searches just the child elements for elements with name. Note: This is useful in scenarios where you want to limit the search to one level down.

These return the matching elements as an IEnumerable<XElement>. You can then use Linq methods (or a foreach loop) to do whatever you want with these elements (such as outputting the values).

Here’s an example of parsing an XML string with XElement and then using XElement.Descendants() to find elements named Movie:

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

var xmlString = @"
<Movies>
	<Movie>Office Space</Movie>
	<Movie>John Wick</Movie>
	<Movie>The Big Short</Movie>
</Movies>";

var moviesXml = XElement.Parse(xmlString);
var movieCount = moviesXml.Descendants("Movie").Count();

Console.WriteLine($"There are {movieCount} movies");
Code language: C# (cs)

This outputs the count of elements found:

There are 3 moviesCode language: plaintext (plaintext)

Get element values

Use the XElement.Value property to get the element’s value as a string.

Here’s an example of getting elements named Title and then outputting their values:

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

var xmlString = @"
<Movies>
	<Movie>
		<Title>Office Space</Title>
		<Year>1999</Year>
	</Movie>
	<Movie>
		<Title>John Wick</Title>
		<Year>2014</Year>
	</Movie>
	<Movie>
		<Title>The Big Short</Title>
		<Year>2015</Year>
	</Movie>
</Movies>";

var moviesXml = XElement.Parse(xmlString);

foreach(var titleElement in moviesXml.Descendants("Title"))
{
	Console.WriteLine(titleElement.Value);
}
Code language: C# (cs)

Notice that XElement.Descendants() is able to find the elements named ‘Title’ two levels down. This is because it recursively searches all descendants in the hierarchy.

This outputs the movie titles:

Office Space
John Wick
The Big ShortCode language: plaintext (plaintext)

Find elements and change their values

Once you find the elements you want with XElement, there’s two ways to change the values:

  • Set the element’s string value with XElement.Value.
  • Use XElement.SetValue() to set the element’s value to a non-string value. It converts the object to a string for you.

Furthermore, you can use XElement.Element() to get the first child element by name. This is useful when you want to get/change multiple child element values.

Here’s an example of getting Movie elements and then changing the values in two of their child elements:

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

var xmlString = @"
<Movies>
	<Movie>
		<Title>Office Space</Title>
		<Streamable>false</Streamable>
	</Movie>
	<Movie>
		<Title>John Wick</Title>
		<Streamable>false</Streamable>
	</Movie>
</Movies>";

var moviesXml = XElement.Parse(xmlString);

foreach (var movie in moviesXml.Descendants("Movie"))
{
	movie.Element("Title").Value = movie.Element("Title").Value.ToUpper();
	
	//Use SetValue() for non-strings
	movie.Element("Streamable").SetValue(true);
}

Console.WriteLine(moviesXml.ToString());
Code language: C# (cs)

This outputs the following XML with changed values:

<Movies>
  <Movie>
    <Title>OFFICE SPACE</Title>
    <Streamable>true</Streamable>
  </Movie>
  <Movie>
    <Title>JOHN WICK</Title>
    <Streamable>true</Streamable>
  </Movie>
</Movies>
Code language: HTML, XML (xml)

Leave a Comment