XML Documents and DataSet in ASP.NET


In this post on XML Documents and DataSet in ASP.NET, I will explain how to create an XML Document from a DataSet and vice versa. Also, I will explain how to display XML data in a data-bound control like GridView.

An Example Demonstrating XML Documents and DataSet

The following example shows how to work with XML in ASP.NET. Therefore, we create a database in SQL Server. Further, we create a table named Items.

Specifying Connection String in web.config

Since we create our DataSet by fetching data from an SQL Server database, we use the ConnectionString property of the database. Therefore, we add the <connectionString> tag to the web.config file to specify a connection string. The following figure shows how to add it in the web.config file of the application.

ConnectionString Property in Web.config File
ConnectionString Property in Web.config File

To clarify further, the properties of a database can be accessed from the Server Explorer available in View menu of Visual Studio. So you can right click on the name of the database, select properties option, and get corresponding properties in the Properties Window. As an illustration, the database table Items is shown below.

Items Table in the Database
Items Table in the Database

So, we create a Web Form having two GridView controls and two Button controls. The following code shows the ASPX page containing these controls.


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="XMLAndDataSets.WebForm1" %>

<!DOCTYPE html>

<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
            <asp:GridView ID="GridView1" runat="server"></asp:GridView>
            <br />
            <asp:Button ID="Button1" runat="server" Text="Create XML Document" OnClick="Button1_Click" />
        <hr />
            <br />
            <asp:Button ID="Button2" runat="server" Text="Convert XML into DataSet" OnClick="Button2_Click" />
            <br />
            <br />
            <asp:GridView ID="GridView2" runat="server"></asp:GridView>

At first, we create a method Connect() that creates a connection to the database. After that, we create a method BindAndDisplay() that binds the first GridView control to the database data. Further, call this method in the Page_Load event handler so that, the web page displays GridView with data when the page is loaded.

Also, the Button1_Click event handler creates an XML file with the name items.xml when the first button is clicked.


using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace XMLAndDataSets
    public partial class WebForm1 : System.Web.UI.Page
        SqlConnection c1;
        SqlDataAdapter da;
        DataSet ds;
        protected void Page_Load(object sender, EventArgs e)
        public void Connect()
            c1 = new SqlConnection(ConfigurationManager.ConnectionStrings["conn1"].ToString());
        public void BindAndDisplay()
            da = new SqlDataAdapter("select * from Items", c1);
            ds = new DataSet("ItemRecords");
            da.Fill(ds, "Items");
            GridView1.DataSource = ds;
        protected void Button1_Click(object sender, EventArgs e)
            string path = Server.MapPath("XMLFiles");
            string filepath = path + @"items.xml";

        protected void Button2_Click(object sender, EventArgs e)
            DataSet ds1 = new DataSet();
            string path1 = Server.MapPath(@"");
            string filepath1 = path1 + @"XMLFile1.xml";
            GridView2.DataSource = ds1;


Synchronizing XML Documents and DataSet in ASP.NET
Synchronizing XML Documents and DataSet in ASP.NET

As shown below, the WriteXML() method of the DataSet class generates the following XML file.

Generated File – items.xml

<?xml version="1.0" standalone="yes"?>
    <item_name>Watch                                                                                               </item_name>
    <item_name>Smart Phone                                                                                         </item_name>
    <item_name>Power Bank                                                                                          </item_name>
    <item_name>Webcam                                                                                              </item_name>
    <item_name>Temperature Sensor                                                                                  </item_name>
    <item_name>Fitness Band                                                                                        </item_name>
    <item_name>Headphone                                                                                           </item_name>
    <item_name>Pen Drive                                                                                           </item_name>
    <item_name>USB Charger                                                                                         </item_name>
    <item_name>USB Torch                                                                                           </item_name>

Converting XML File into DataSet

For the purpose of transforming an XML file into DataSet, we create an XML file. Therefore, we select Add New Item option from the Project menu in Visual Studio and select the XML File option from there.

After that, we create the XML file that includes the Persons data which will form the DataSet.

<?xml version="1.0" encoding="utf-8" ?>

As shown below, clicking on the second button calls the ReadXML() method of the DataSet. Once, this method is called, we get a DataSet object. Finally, we bind the second GridView control using that DataSet object.

Converting XML to DataSet
Converting XML to DataSet

To summarize, XML Documents and DataSet in ASP.NET work interchangeably in ASP.NET. What’s more it very easy to do so with built-in methods of the DataSet class. Therefore, we can bind XML data to a web control by creating a DataSet object from the XML file.

Further Reading

Parameter and ParameterCollection in ADO.NET

Database Manipulation Using DataGrid

Example of Button and Link Button Control in ASP.NET

Example of Chart Control in ASP.NET

Creating a DataTable from a DataReader in ASP.NET

Deleting a Record using DataGrid Control in ASP.NET

Edit a Record Using DataGrid Control in ASP.NET

Insert a Record Using ItemCommand Event in DataGrid

CRUD Operations with DataGrid in ASP.NET

Creating Columns in a DataGrid Control

XML Documents and DataSet in ASP.NET

Code Render Block in ASP.NET

ASP.NET Core Features and Advantages

Display Images Using DataList Control

Adding Images Using Image Control

Creating a Group of Radio Buttons Using RadioButtonList Control

Example of Button Control in ASP.NET

Using MD5 Hash Algorithm

ItemDataBound Event in DataList

More Features of DataList in ASP.NET

A Simple Example of Using a DataList Control in ASP.NET

Properties and Methods of DataList Control in ASP.NET

ASP.NET Practice Exercise

Exploring DataList Control in ASP.NET

Custom Validator Control in ASP.NET

Validation Summary Control in ASP.NET

Validation Controls Examples – RequiredFieldValidator, CompareValidator, and RangeValidator

An Example of Data Binding with RadioButtonList Control

Binding Data to Web Control in ADO.NET

Examples of AdRotator Control in ASP.NET

Examples of Validation Controls in ASP.NET

Overview of MVC architecture in ASP.NET


You may also like...