.NET Exceptions – System.Xml.XmlException

Taking the next step in our journey through the vast .NET Exception Handling series, today we’re taking a closer look at the System.Xml.XmlException class. As you might suspect from the name, the System.Xml.XmlException is the generic error that is thrown when most XML-related errors occur, such as failure to parse malformed XML content.

In this article we’ll examine the System.Xml.XmlException by looking at where it resides in the .NET exception hierarchy. We’ll also go over some fully functional C# code examples that will illustrate how XML might be commonly used, and how that can potentially lead to System.Xml.XmlExceptions in your own code. Let’s get this party started!

The Technical Rundown

Full Code Sample

Below is the full code sample we’ll be using in this article. It can be copied and pasted if you’d like to play with the code yourself and see how everything works.

When Should You Use It?

There’s no time like the present, so let’s just jump right into our code sample and get to working with some XML! For our example code we have a small collection of books contained within a few local XML documents, books.xml and books-malformed.xml:

It’s worth noting that, if you manually add a file to your Visual Studio project (such as books.xml), you’ll likely need to adjust the file properties and set the Copy to Output Directory flag to Copy always or Copy if newer. This will ensure that the project file in question is locally available to your executing assembly.

With our book data in hand, we want to extract that data and use it in code. Our goal is to create a series of Book class instances for each book element found in our XML documents. Here’s the simple Book class we’ll be using:

In order to parse our XML we have many options, including LINQ to XML. However, our example XML documents are fairly basic in structure, so we don’t need any fancy queries in this particular tutorial. To read the XML and create our Book instances we’ve created the CreateBooksFromXml(string path) method:

This method starts by attempting to create an XElement from the passed XML path, then retrieves the IEnumerable<XElement> collection of elements with the corresponding element name of Book. We then iterate over that collection of elements and create a new Book class instance for each element, passing the appropriate XML element values to the corresponding arguments of the Book constructor. Since the XElement.Value property is a string data type, we need to explicitly convert the PageCount property element to an int using int.TryParse(). Finally, once our Book instance is created we output it to the log.

The Program.Main(string[] args) method tests out our functionality two times, once for each respective XML document:

As you might suspect from the XML document names, the first call using books.xml works just fine, creating new Book instances for each parsed XML element and outputting the Book instance objects to the log:

However, the second call using books-malformed.xml throws a System.Xml.XmlException at us, indicating the position of the character that caused the issue:

You may have picked up on it already, but the problem is a missing greater-than sign (>) following the opening PageCount element in the first Book element of Dune:

Obviously, we can fix this by adding that missing greater-than sign back. Doing so and running our code again produces the proper output we’re after for the books-malformed.xml parse:

To get the most out of your own applications and to fully manage any and all .NET Exceptions, check out the Airbrake .NET Bug Handler, offering real-time alerts and instantaneous insight into what went wrong with your .NET code, along with built-in support for a variety of popular development integrations including: JIRA, GitHub, Bitbucket, and much more.