---
title: "How to read XML files in C#? Please provide a sample."  
description: "How to read XML files in C#? Please provide a sample."  
author: "Utpal Vishwas"  
published: 2023-09-27  
updated: 2023-09-28  
canonical: https://www.mindstick.com/forum/159945/how-to-read-xml-files-in-c-sharp-please-provide-a-sample  
category: "c#"  
tags: ["c#", "xml", "xml reader"]  
reading_time: 2 minutes  

---

# How to read XML files in C#? Please provide a sample.

How to read [XML](https://www.mindstick.com/blog/203/working-with-xml-data-in-sql-server) [files in C#](https://www.mindstick.com/interview/34085/how-do-you-handle-exceptions-when-working-with-files-in-c-sharp)? Please provide a sample.

## Replies

### Reply by Aryan Kumar

In C#, you can read XML [files](https://www.mindstick.com/articles/23302/the-importance-and-advantage-of-keeping-your-important-files-on-the-cloud) using the **System.Xml** namespace, which provides classes like **XmlDocument**, **XmlTextReader**, and **XDocument** for parsing and working with XML data. Here, I'll provide a sample using both **XmlDocument** and **XDocument** to read an XML file:

## Sample XML File (example.xml):

```plaintext
<?xml version="1.0" encoding="UTF-8"?>
<root>
  <person>
    <name>John Doe</name>
    <age>30</age>
  </person>
  <person>
    <name>Jane Smith</name>
    <age>25</age>
  </person>
</root>
```

## Reading XML using XmlDocument:

```plaintext
using System;
using System.Xml;

class Program
{
    static void Main()
    {
        // Load the XML file
        XmlDocument doc = new XmlDocument();
        doc.Load("example.xml");

        // Select the root node
        XmlNode root = doc.DocumentElement;

        // Select child nodes
        XmlNodeList personNodes = root.SelectNodes("person");

        // Iterate through the person nodes
        foreach (XmlNode personNode in personNodes)
        {
            string name = personNode.SelectSingleNode("name").InnerText;
            int age = int.Parse(personNode.SelectSingleNode("age").InnerText);

            Console.WriteLine($"Name: {name}, Age: {age}");
        }
    }
}
```

## Reading XML using XDocument (LINQ to XML):

```plaintext
using System;
using System.Linq;
using System.Xml.Linq;

class Program
{
    static void Main()
    {
        // Load the XML file
        XDocument doc = XDocument.Load("example.xml");

        // Query elements using LINQ to XML
        var persons = from person in doc.Descendants("person")
                      select new
                      {
                          Name = (string)person.Element("name"),
                          Age = (int)person.Element("age")
                      };

        // Iterate through the selected elements
        foreach (var person in persons)
        {
            Console.WriteLine($"Name: {person.Name}, Age: {person.Age}");
        }
    }
}
```

Both **XmlDocument** and **XDocument** provide methods for loading and querying XML data. **XDocument** is part of LINQ to XML and is often preferred for its simplicity and integration with LINQ. Choose the one that best fits your needs and coding style.


---

Original Source: https://www.mindstick.com/forum/159945/how-to-read-xml-files-in-c-sharp-please-provide-a-sample

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
