C# and .Net are rich in methods to complete the task. With each new version we have new classes and methods. Reading and parsing XML is not an exclusion. Below I’ll tell you about 3 methods for reading XML-data in C#.

First of all, we have an XML file and it’s name is "companies.xml". It’s a list of companies and their properties. As you can see, some items have different kinds of properties.


Method 1. Parse XML using XmlDocument

XmlDocument is presented in .Net Framework since 3.5 in Assembly "System.Xml (System.Xml .dll)".

Below there’s a code to display a field "short_name" of company. In this code we load the XML document from the file using method "Load".  After we set the value of "xpath" variable, it’s based on a structure of XML document, in our case it’s "companies/company". Then we get a list of nodes that respond to the set xpath and send them to LiteralControl.


In this case we need to write each property to display and thinks about exceptions. It the value of a property is "False" than we have exception.  The error is something like this:

XML document error

XML document error


One of solutions is to use "try-catch" in a cycle:


Method 2 – Parse XML using XmlReader


XmlReader is also presented in System.Xml assembly since .Net Framework 3.5.

Using XML-reader you can find an element in your XML document and make some actions with it. Using method "ReadToFollowing(String)" you can read XML until an element with the specified qualified name is found and, for example, to add it to string variable using "ReadElementContentAsString". There are a lot of ReadElementAsXXX, it’s described on MSDN page .


This method is nice if you need to get not a lot of field values from XML document. It’s rather quick to write a code like this and to use it. But I don’t recommend you to use this method for XML documents with difficult structure.


Method 3 – Parse XML using XmlSerializer

XmlSerializer is the most powerful tool to parse XML in C#. This Class exists in namespace  System.Xml.Serialization and in assembly System.Xml.dll since .Net Framework 3.5.

In MSDN (http://msdn.microsoft.com/ru-ru/library/system.xml.serialization.xmlserializer(v=vs.110).aspx) there are good descriptions of what serialization and deserialization are

XML serialization is the process of converting an object's public properties and fields to a serial format (in this case, XML) for storage or transport.

Deserialization re-creates the object in its original state from the XML output. You can think of serialization as a way of saving the state of an object into a stream or buffer.

To use deserialization, at first you need to create classes according to your XML document.


I think it’s the most time-consuming process.


As a result, you have a table with data of your XML document.

If XML document has nested nodes, you just need to create more classes and arrays or lists.

For example, let’s add code in "<codes>" to one of <company> items.


Add to class Company this code:


And create class named "Code"