C# – How to parse XML with XElement (Linq)

Use the XElement class (from the Linq-to-Xml API) to parse XML and work with it in memory. You can use this to search for XML elements, attributes, and modify values. This is an alternative to deserializing to an object (which requires you to define a class that matches the XML structure).

I’ll show examples of using XElement to parse XML from a string and from a file. At the end, I’ll explain when to use XDocument instead of XElement.

Parse an XML string with XElement

Use XElement.Parse() to parse an XML string. This returns an XElement object.

Here’s an example of parsing an XML string and outputting the element values:

using System.Xml.Linq; var xmlString = """ <Movie> <Title>Office Space</Title> <Year>1999</Year> </Movie> """; var movie = XElement.Parse(xmlString); var title = movie.Element("Title").Value; var year = movie.Element("Year").Value; Console.WriteLine($"{title} was released in {year}");
Code language: C# (cs)

This outputs the following:

Office Space was released in 1999
Code language: plaintext (plaintext)

Parse an XML file with XElement

To parse an XML file, use XElement.Load() and specify the file path (absolute or relative). This returns an XElement object.

Here’s an example. Let’s say you have the following XML file (named movies.xml):

<Movies> <Movie genre="comedy">Office Space</Movie> <Movie genre="action">John Wick</Movie> <Movie genre="action">Jurassic Park</Movie> </Movies>
Code language: HTML, XML (xml)

The following shows how to parse this file with XElement. It’s outputting the number of elements with a specific attribute value:

using System.Linq; using System.Xml.Linq; var moviesXml = XElement.Load("movies.xml"); //How many action movies? var count = moviesXml.Descendants("Movie").Count(m => m.Attribute("genre").Value == "action"); Console.WriteLine($"There are {count} action movies");
Code language: C# (cs)

This outputs the following:

There are 2 action movies
Code language: plaintext (plaintext)

Parsing an XML Document with XDocument

If you’re dealing with an XML Document, use the XDocument class (from the Linq-to-Xml API) to parse it instead of XElement. An XML Document has features such as document type declaration and comments. XDocument handles these and preserves them, while XElement doesn’t (which can lead to surprises).

Here’s an example. Let’s say you have the following XML file with XML Document features (highlighted):

<?xml version="1.0" encoding="utf-8"?> <Movies> <!--These should all be streamable --> <Movie id="123" streamable="false">Office Space</Movie> <Movie id="456" streamable="false">John Wick</Movie> <Movie id="789" streamable="false">Jurassic Park</Movie> </Movies>
Code language: HTML, XML (xml)

The following shows how to parse this XML file with XDocument.Load(), modify some values, and then overwrite the file with the changes:

using System.Xml.Linq; var moviesXMLDoc = XDocument.Load("movies.xml"); foreach (var movie in moviesXMLDoc.Descendants("Movie")) { movie.SetAttributeValue("streamable", true); } moviesXMLDoc.Save("movies.xml");
Code language: C# (cs)

This outputs the following changed XML to the file while preserving the document features (highlighted):

<?xml version="1.0" encoding="utf-8"?> <Movies> <!--These should all be streamable --> <Movie id="123" streamable="true">Office Space</Movie> <Movie id="456" streamable="true">John Wick</Movie> <Movie id="789" streamable="true">Jurassic Park</Movie> </Movies>
Code language: HTML, XML (xml)

Leave a Comment