In this article I will explain with an example, how to export data from Database to XML file in ASP.Net using C# and VB.Net.
 
 
Database
I have made use of the following table Customers with the schema as follows.
Export data from Database to XML in ASP.Net using C# and VB.Net
 
I have already inserted few records in the table.
Export data from Database to XML in ASP.Net using C# and VB.Net
 
Note: You can download the database table SQL by clicking the download link below.
         Download SQL file
 
 
HTML Markup
The following HTML Markup consists of a Button.
The Button has been assigned an OnClick event handler.
<asp:Button Text="Export" OnClick="ExportXML" runat="server" />
 
 
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
 
VB.Net
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
 
 
Exporting DataSet to XML File
When the Export Button is clicked, the following event handler is executed.
Inside the event handler the DataSet is populated with records from the database table named Customers and the name is set for it i.e. Customers.
This name is used as root element i.e. the parent node for the XML file.
Then the TableName property is set for the DataTable i.e. Customer. This name will be used to set the child element i.e. the child node for the XML file.
Then, following properties are set in the Response class.
1. ContentType – It informs the Browser about the file type. In this case it is XML.
2. Content-Disposition – It is a response header indicating, the download file is an attachment and allows setting the file name.
Once the Response properties are set, the DataSet is converted to an XML string using the GetXml method of DataSet class.
Finally, using the Write method of the Response class, XML string is exported and downloaded as XML file.
C#
protected void ExportXML(object sender, EventArgs e)
{
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand("SELECT CustomerId, Name, Country FROM Customers", con))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
            {
                using (DataSet ds = new DataSet("Customers"))
                {
                    sda.Fill(ds);
                    ds.Tables[0].TableName = "Customer";
 
                    //Download the XML file.
                    Response.Clear();
                    Response.Buffer = true;
                    Response.Charset = "";
                    Response.Cache.SetCacheability(HttpCacheability.NoCache);
                    Response.ContentType = "application/xml";
                    Response.AddHeader("content-disposition", "attachment;filename=Customers.xml");
                    Response.Write(ds.GetXml());
                    Response.Flush();
                    Response.End();
                }
            }
        }
    }
}
 
VB.Net
Protected Sub ExportXML(ByVal sender As bject, ByVal e As EventArgs)
    Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con As SqlConnection = New SqlConnection(constr)
        Using cmd As SqlCommand = New SqlCommand("SELECT CustomerId, Name, Country FROM Customers", con)
            Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
                Using ds As DataSet = New DataSet("Customers")
                    sda.Fill(ds)
                    ds.Tables(0).TableName = "Customer"
 
                    'Download the XML file.
                    Response.Clear()
                    Response.Buffer = True
                    Response.Charset = ""
                    Response.Cache.SetCacheability(HttpCacheability.NoCache)
                    Response.ContentType = "application/xml"
                    Response.AddHeader("content-disposition", "attachment;filename=Customers.xml")
                    Response.Write(ds.GetXml())
                    Response.Flush()
                    Response.End()
                End Using
            End Using
        End Using
    End Using
End Sub
 
 
Screenshots
The Form
Export data from Database to XML in ASP.Net using C# and VB.Net
 
The generated XML file
Export data from Database to XML in ASP.Net using C# and VB.Net
 
 
Demo
 
 
Downloads