Blog

Export/Download Data into XML from Data table/Dataset

By Pankaj Pandey Blogs | .NET 4.5 Apr 09, 2013
Here I am giving code to export and download data from data table or data set to XML file.
Step 1>
Connect to your databse and put a button on your form
step 2> Add given namespace 

using System.Data;
using System.Data.SqlClient;
using System.IO;

step 3>Copy and paste given code on button click event

SqlCommand cmd = new SqlCommand("select * FROM [table]", conn);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        dt.TableName = "Records";
        da.Fill(dt);
        DataSet dS = new DataSet();
        dS.DataSetName = "RecordSet";
        dS.Tables.Add(dt);
        StringWriter sw = new StringWriter();
        dS.WriteXml(sw, XmlWriteMode.IgnoreSchema);
        string s = sw.ToString();
       
        string attachment = "attachment; filename=test.xml";
        Response.ClearContent();
        Response.ContentType = "application/xml";
        Response.AddHeader("content-disposition", attachment);
        Response.Write(s);
        Response.End(); 

Step 4> Click on like button if its helpful for you 
COMMENT USING