How to Read XML Documents in C# (2024)

In this article, we’ll talk about how to read XML Documents in C#. In the preceding article, we addressed the creation of custom XML documents.

Also, we have already explored how to serialize and deserialize objects to and from XML in the articles titled Serializing Objects to XML in C# andXML Deserialization in C#. So what else can we learn?

To download the source code for this article, you can visit our GitHub repository.

Reading XML Documents

NOTE: The code will employ the classes Person, People, and CreateXMLUsingXmlWriter, developed in the preceding article, and we won’t duplicate the code here.

Similar to creating XML documents, where we could choose between LinqToXml or XmlWriter, we once again have two potential approaches: utilizing the XDocument class or opting for the older XmlReader class.

The XDocument path is simpler and more user-friendly, while XmlReader provides greater control over the reading process.

Support Code Maze on Patreon to get rid of ads and get the best discounts on our products!

Using XDocument to Read XML Documents in C#

Reading XML using XDocument couldn’t be simpler. Whether we are reading XML from a file or a string, the XDocument class provides functions for these scenarios.

To read an XML document from a string, we use the Parse() method. To read an XML from a file, we use the Load() method.

If the document is not syntactically correct, these methods will throw an exception:

public class ReadingXmlUsingXDocument{ public XDocument ReadXmlAndCatchErrors(string xml) { try { return XDocument.Parse(xml); } catch (Exception ex) { Console.WriteLine("Something went wrong:"); Console.WriteLine(ex); } return new XDocument(); }}

We’ve created a ReadingXmlUsingXDocument class utilizing the XDocument class. This class features a method, ReadXmlAndCatchErrors(), which parses the given XML string. If any errors occur during parsing, the method catches the error, displays it in the console, and returns an empty XDocument.

To evaluate this method, we can generate XML documents using the previously developed CreateXMLUsingXmlWriter class. First, we create a valid XML document and then proceed to read it:

public static string TestValidXml(){ var xmlDoc = ReadXmlAndCatchErrors(CreateXMLUsingXmlWriter.CreateSimpleXML(People.GetOne())); return xmlDoc.ToString();}

Everything unfolds as anticipated. The document is successfully created, and we can both read its content and output it to the console.

But what happens if we provide invalid XML:

public static string TestInvalidXml(){ var xmlDoc = ReadXmlAndCatchErrors(CreateXMLUsingXmlWriter.CreateWrongXML(People.GetOne())); return xmlDoc.ToString();}

XDocument.Parse() will throw an exception and our ReadXmlAndCatchErrors() method catches it and returns an empty XDocument.

Getting Values From XDocument

With the XML document now within the XDocument object, the question arises: How do we extract the name or age of a person from the XDocument object?

As this extends beyond the scope of this article, we will briefly outline a few options using the initially created ‘people document’:

<person> <name> <firstName>Emma</firstName> <lastName>Brown</lastName> </name> <email>[emailprotected]</email> <age>58</age></person>

XML document contains data about a person. Among them are the email and the age of the person we want to extract.

Reading With the Use of the Element Collection

Every element inside XDocument has an element collection of all its children. If an element has no children, this collection will be empty.

We traverse through the XML tree by referencing the elements by name and then read their values:

public static string TestReadWithElementCollection(){ var xmlDoc = ReadXmlAndCatchErrors(CreateXMLUsingXmlWriter.CreateSimpleXML(People.GetOne())); var name = xmlDoc.Root!.Element("name")!.Element("firstName")!.Value; var age = xmlDoc.Root!.Element("age")!.Value; return $"Name: {name}, Age: {age}";}

We start from the root of the document, which is a <person> element. There we search for the <name> element, and inside the <name> element, we find the <firstName> element. From the <firstName> element, we take the value, which is, of course, the name of the person.

The same is true for the age.

Notice that here we are using the ‘!’ operator, as we know in our example case, that the XML document contains all the elements we are searching for.

Using XPath

To retrieve data from an XML document, we can also utilize XPath. The XPath language is quite extensive, but in this article, we will provide a simple example of reading the name and age fields:

public static string TestReadUsingXPath(){ var xmlDoc = ReadXmlAndCatchErrors(CreateXMLUsingXmlWriter.CreateSimpleXML(People.GetOne())); var name = xmlDoc.XPathSelectElement("/person/name/firstName")!.Value; var age = xmlDoc.XPathSelectElement("/person/age")!.Value; return $"Name: {name}, Age: {age}";}

In this example, we use XPath to locate elements like navigating a file system. For instance, to access the <firstName> element, we use the path “/person/name/firstName.”

There are various ways to express XPath queries, such as:

  • //firstName

  • /person/name/*[1]

  • /person/name/*[local-name()='firstName']

The first XPath expression selects all <firstName> elements from anywhere in the XML document. The second selects the first child element under the <name> element within the <person> element. The last one selects the first child element under the <name> element within the <person> element, specifically if its local name is <firstName>, disregarding the namespace.

XPath is an extensive language, and the examples given here offer only a brief overview. For a more in-depth exploration of this topic, check out our in-depth article: Selecting Xml Nodes With XPath.

Using XmlReader to Read XML Documents in C#

The second option mentioned for reading XML is the XmlReader class.

The XmlReader class parses the XML one element at a time. However, here we are not talking only about elements such as <name>, but also elements such as Attribute, Whitespace, Text, CDATA, and others.

All these possibilities are described in an XmlNodeType enum. We use a reader to read one element after another sequentially, and the reader will give us the next element and its type, value, etc:

public static IEnumerable<string> ReadXml(string xml){ using var reader = XmlReader.Create(new StringReader(xml)); List<string> result = []; while (reader.Read()) { result.Add($"> {reader.NodeType} | {reader.Name} | {reader.Value}"); } return result;}

Running this method will produce a long list of different types, but Whitespace type may ruin the format as every new line is also a Whitespace.

Let’s remove the whitespaces and see what we get:

public static IEnumerable<string> ReadXmlWithoutWhiteSpace(string xml){ using var reader = XmlReader.Create(new StringReader(xml)); List<string> result = []; while (reader.Read()) { if (reader.NodeType == XmlNodeType.Whitespace) continue; result.Add($"> {reader.NodeType} | {reader.Name} | {reader.Value}"); } return result;}

This method is very similar, except it skips all whitespaces. From our sample XML document, we will get output like this:

> XmlDeclaration | xml | version="1.0" encoding="utf-16"> Element | person |> Element | name |> Element | firstName |> Text | | William> EndElement | firstName |> Element | lastName |> Text | | Taylor> EndElement | lastName |> EndElement | name |> Element | email |> Text | | [emailprotected]> EndElement | email |> Element | age |> Text | | 20> EndElement | age |> EndElement | person |

We can learn a lot by examining the output of the method.

If nothing more, at least even the basic elements like <lastName>Taylor</lastName> is split into three parts: Element, Text, EndElement.

How to Read XML Documents in Practice

Now that we’ve understood the intricacies of reading custom XML documents let’s review a practical example.

We’ll create a method that enables us to traverse through a lengthy XML file filled with names and surnames. The objective is to extract both the first and last names efficiently.

Reading Names and Surnames

We’ve already set up an XML file containing personal data with <firstName> and <lastName> elements. We aim to extract this information and display it in a tabular format.

Firstly, we’ll define a private class, PersonData, with FirstName and LastName properties to store individual names:

private class PersonData{ public string? FirstName { get; set; } public string? LastName { get; set; } public void Init() => FirstName = LastName = "";}

Now, the implementation is straightforward:

public static void ReadNamesAndAges(string xml){ var settings = new XmlReaderSettings { IgnoreWhitespace = true }; using var reader = XmlReader.Create(new StringReader(xml), settings); { var personData = new PersonData(); var numberOfPersons = 0; while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element) { if (reader.Name == "person") personData.Init(); if (reader.Name == "firstName") personData.FirstName = reader.ReadElementContentAsString(); if (reader.Name == "lastName") personData.LastName = reader.ReadElementContentAsString(); } if (reader.NodeType == XmlNodeType.EndElement) { if (reader.Name == "person") Console.WriteLine($"#{++numberOfPersons,3} | " + $"{personData.FirstName,-15} | {personData.LastName,-15}"); } } }}

Initially, we set up the XmlReader to disregard Whitespaces, simplifying the process. Here, we employ yet another option to skip whitespace by utilizing the XmlReaderSettings class.

As we iterate through elements one by one, when we come across the <person> element, we initialize the PersonData object. For <firstName> or <lastName> elements, we update the respective properties.

Upon encountering the </person> end element, we know that we have all the data for a person, allowing us to print it out. This leads to a well-organized table of people from our XML file:

# 1 | Olivia | Davis# 2 | John | Davis# 3 | Sarah | Moore# 4 | Sarah | Smith

The outcome is a well-organized table displaying the sequence number of a person along with their first and last names.

Conclusion

Despite JSON becoming the de facto standard for API communication, XML remains integral to many industries due to its longstanding presence. As we inevitably encounter XML standards, we must familiarize ourselves with XML handling in .NET, utilizing classes such as XDocument, XmlDocument, and XmlReader.

XDocument is a versatile tool for most tasks, simplifying the reading of diverse XML documents. For complex or poorly structured XML documents, the advanced capabilities of XmlReader are essential.

Regardless of your content or its complexity, mastering these tools is key to effective XML handling in .NET.

Liked it? Take a second to support Code Maze on Patreon and get the ad free reading experience!

How to Read XML Documents in C# (2024)

FAQs

How to Read XML Documents in C#? ›

To read an XML document from a string, we use the Parse() method. To read an XML from a file, we use the Load() method. We've created a ReadingXmlUsingXDocument class utilizing the XDocument class. This class features a method, ReadXmlAndCatchErrors() , which parses the given XML string.

How to read file XML in C#? ›

How to read XML data from a URL
  1. Copy the Books. ...
  2. Open Visual Studio.
  3. Create a new Visual C# Console Application. ...
  4. Specify the using directive on the System. ...
  5. Create an instance of the XmlTextReader class, and specify the URL. ...
  6. Read through the XML. ...
  7. Inspect the nodes. ...
  8. Inspect the attributes.
Apr 4, 2023

How do I read an XML document? ›

Opening XMLs Fast
  1. Right click the XML file you want to open and hover the cursor over “open with.” Select Notepad to open the text on your computer.
  2. You can open XML files using a browser if your notepad isn't legible.
  3. If the contents of the XML file don't make sense, download an XML reader to interpret the file.

How to display XML data in C#? ›

One trick that it will help to know is that you can copy the contents of a XML file into the clipboard then go to a C# (. cs) file and use Edit | Paste Special. You will have the option to paste XML as C# class(es). Then you can use the XmlSerializer class to read and write XML.

How to read data from XML file to DataSet in C#? ›

Load Xml Document in Dataset
  1. class Program.
  2. public static void Main(string[] args)
  3. string xmlDoc = @"<? xml version='1.0'?>
  4. StringReader sr = new StringReader(xmlDoc);
  5. DataSet ds = new DataSet();
  6. foreach (DataColumn dc in ds.Tables[0].Columns)
  7. Console.Write("{0,15}", dc.ColumnName);
  8. Console.Write("\n");
Oct 4, 2013

How to get XML value in C#? ›

We have a scenario to get the value of the tag in XML, so here is how you can get the required value from the XML. XmlDocument xdoc = new XmlDocument(); xdoc. LoadXml(fetchXML); XmlNodeList nodeList = xdoc.

How to use XML documentation in C#? ›

Documentation comments are similar to C# single-line comments, but start with /// (that's three slashes), and can be applied to any user-defined type or member. As well as containing descriptive text, these comments can also include embedded XML tags.

How to read XML file in .NET Core? ›

Reading XML using LINQ to XML
  1. Load the XML file.
  2. Use LINQ to XML to search for an element named appSettings and its descendants named add.
  3. Project the XML into an array of an anonymous type with a Key and Value property.
  4. Enumerate through the array to show the results:

How to view XML files? ›

If you want to open an XML file and edit it, you can use a text editor. You can use default text editors, which come with your computer, like Notepad on Windows or TextEdit on Mac. All you have to do is locate the XML file, right-click the XML file, and select the "Open With" option.

How to read XML file and convert to string in C#? ›

We can use the built-in methods of C# to read XML documents and convert them to strings easily. We will use the XmlDocument class from the System. Xml namespace to load the XML document, and use the OuterXml property to get the XML content as a string.

How to access data from XML? ›

You can parse XML documents from an origin system with an origin enabled for the XML data format. You can also parse XML documents in a field in a Data Collector record with the XML Parser processor. You can use the XML data format and the XML Parser to process well-formed XML documents.

How to read XML file and insert into database in C#? ›

Once we get the DataTable, we will assign it a name similar to the name of our XML file. In the below function, we will generate the DataTable from the XML file by using simple XmlDocument code. In this function, we are extracting XML nodes and by using these nodes we are structuring the DataTable.

How to read XML file and store in list in C#? ›

Using "(Entry)serializer. Deserialize(reader)" convert the specific file data to C# object according to created Entry class. And after completing the foreach loop, you get the whole XML files data into listAllEntries object.

How to read and update XML file in C#? ›

How to read XML Documents?
  1. XmlTextReader textReader = new XmlTextReader("C:\\books.xml"); ...
  2. XmlTextReader textReader = new XmlTextReader("C:\\books.xml"); ...
  3. // Create a new file in C:\\ dir XmlTextWriter textWriter = new XmlTextWriter("C:\\myXmFile.xml", null) ; ...
  4. textWriter.
Oct 4, 2023

How to read XML file using serialization in C#? ›

XmlSerializer to serialize it.
  1. public T DeserializeToObject<T>(string filepath) where T : class.
  2. {
  3. System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(typeof(T));
  4. using (StreamReader sr = new StreamReader(filepath))
  5. {
  6. return (T)ser.Deserialize(sr);
  7. }
  8. }
Jan 24, 2020

References

Top Articles
Latest Posts
Article information

Author: Jeremiah Abshire

Last Updated:

Views: 6068

Rating: 4.3 / 5 (74 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Jeremiah Abshire

Birthday: 1993-09-14

Address: Apt. 425 92748 Jannie Centers, Port Nikitaville, VT 82110

Phone: +8096210939894

Job: Lead Healthcare Manager

Hobby: Watching movies, Watching movies, Knapping, LARPing, Coffee roasting, Lacemaking, Gaming

Introduction: My name is Jeremiah Abshire, I am a outstanding, kind, clever, hilarious, curious, hilarious, outstanding person who loves writing and wants to share my knowledge and understanding with you.