---
title: "ADO.NET using XML Data"  
description: "ADO.NET allows you to work with XML data in multiple ways, including storing, retrieving, and manipulating XML within databases or datasets."  
author: "ICSM Computer"  
published: 2025-02-13  
updated: 2025-02-13  
canonical: https://www.mindstick.com/articles/338521/ado-dot-net-using-xml-data  
category: "ado.net"  
tags: ["c#", "ado.net"]  
reading_time: 3 minutes  

---

# ADO.NET using XML Data

ADO.NET allows you to work with [XML data](https://www.mindstick.com/forum/131/problem-in-showing-the-xml-data-in-well-formed) in multiple ways, including storing, retrieving, and manipulating XML within [databases](https://www.mindstick.com/blog/304190/10-best-nosql-databases) or datasets. Here are some key aspects of working with XML in ADO.NET:

![ADO.NET using XML Data](https://www.mindstick.com/mindstickarticle/6ef2ee43-bf2c-488a-bde5-e5377d0e3f19/images/b5ecb514-d48a-4346-ac07-45fd4c62a7ed.png)

#### 1. Reading and Writing XML with DataSet

ADO.NET provides built-in support for working with XML through the `DataSet` class.

## Writing DataSet to XML

```cs
using System;
using System.Data;

class Program
{
    static void Main()
    {
        // Create a sample DataSet
        DataSet ds = new DataSet("SampleDataSet");
        DataTable dt = new DataTable("Employees");

        // Add columns
        dt.Columns.Add("ID", typeof(int));
        dt.Columns.Add("Name", typeof(string));
        dt.Columns.Add("Department", typeof(string));

        // Add rows
        dt.Rows.Add(1, "John Doe", "HR");
        dt.Rows.Add(2, "Jane Smith", "IT");

        // Add table to DataSet
        ds.Tables.Add(dt);

        // Write to XML file
        ds.WriteXml("employees.xml", XmlWriteMode.WriteSchema);

        Console.WriteLine("XML file created successfully.");
    }
}
```

## Reading XML into DataSet

```cs
using System;
using System.Data;

class Program
{
    static void Main()
    {
        DataSet ds = new DataSet();
        ds.ReadXml("employees.xml");

        foreach (DataTable table in ds.Tables)
        {
            foreach (DataRow row in table.Rows)
            {
                Console.WriteLine($"ID: {row["ID"]}, Name: {row["Name"]}, Department: {row["Department"]}");
            }
        }
    }
}
```

####

#### 2. Storing XML in SQL Server

If your [database](https://www.mindstick.com/forum/160888/how-much-does-it-cost-to-get-training-in-database-in-mindstick-training) has an `XML` column, you can [store and retrieve](https://answers.mindstick.com/qa/102391/how-does-a-computer-hard-drive-store-and-retrieve-data) XML data using ADO.NET.

**Inserting XML into [SQL Server](https://www.mindstick.com/articles/12620/use-of-in-operator-in-sql-server)**

```cs
using System;
using System.Data;
using System.Data.SqlClient;

class Program
{
    static void Main()
    {
        string connectionString = "your_connection_string";

        string xmlData = "<Employees><Employee><ID>1</ID><Name>John Doe</Name><Department>HR</Department></Employee></Employees>";

        using (SqlConnection conn = new SqlConnection(connectionString))
        {
            string query = "INSERT INTO EmployeeRecords (XmlData) VALUES (@XmlData)";
            using (SqlCommand cmd = new SqlCommand(query, conn))
            {
                cmd.Parameters.Add("@XmlData", SqlDbType.Xml).Value = xmlData;
                conn.Open();
                cmd.ExecuteNonQuery();
                Console.WriteLine("XML data inserted successfully.");
            }
        }
    }
}
```

## Retrieving XML from SQL Server

```cs
using System;
using System.Data;
using System.Data.SqlClient;
using System.Xml;

class Program
{
    static void Main()
    {
        string connectionString = "your_connection_string";

        using (SqlConnection conn = new SqlConnection(connectionString))
        {
            string query = "SELECT XmlData FROM EmployeeRecords";
            using (SqlCommand cmd = new SqlCommand(query, conn))
            {
                conn.Open();
                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        string xmlData = reader["XmlData"].ToString();
                        Console.WriteLine("XML Data:");
                        Console.WriteLine(xmlData);
                    }
                }
            }
        }
    }
}
```

#### 3. Using `XmlReader` with ADO.NET

Instead of working directly with `DataSet`, you can use `XmlReader` for [better performance](https://answers.mindstick.com/qa/111685/how-do-i-optimize-my-code-for-better-performance) when handling large XML data.

**Reading XML Using** `XmlReader`

```cs
using System;
using System.Data.SqlClient;
using System.Xml;

class Program
{
    static void Main()
    {
        string connectionString = "your_connection_string";

        using (SqlConnection conn = new SqlConnection(connectionString))
        {
            string query = "SELECT XmlData FROM EmployeeRecords";
            using (SqlCommand cmd = new SqlCommand(query, conn))
            {
                conn.Open();
                using (XmlReader reader = cmd.ExecuteXmlReader())
                {
                    while (reader.Read())
                    {
                        Console.WriteLine(reader.ReadOuterXml());
                    }
                }
            }
        }
    }
}
```

#### Key Takeaways

- `DataSet.WriteXml()` and `DataSet.ReadXml()` allow easy XML [data storage](https://answers.mindstick.com/qa/102476/how-does-a-computer-s-raid-configuration-enhance-data-storage) and retrieval.
- SQL Server supports `XML` data type, which can be manipulated using ADO.NET.
- `XmlReader` provides a more [efficient](https://www.mindstick.com/blog/33381/4-software-tools-to-manage-your-remote-employees-in-a-more-efficient-manner) way to read large XML data compared to `DataSet`.

---

Original Source: https://www.mindstick.com/articles/338521/ado-dot-net-using-xml-data

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
