Export/Download Data into XML from Data table/Dataset

By Pankaj Pandey Apr 09 2013
Here I am giving code to export and download data from data table or data set to XML file.
    • Like
    • Love It
    • Awesome
    • Interesting
    • It's Okay
    • Thumbs Down
  • 8.5k
  • 0
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";
        DataSet dS = new DataSet();
        dS.DataSetName = "RecordSet";
        StringWriter sw = new StringWriter();
        dS.WriteXml(sw, XmlWriteMode.IgnoreSchema);
        string s = sw.ToString();
        string attachment = "attachment; filename=test.xml";
        Response.ContentType = "application/xml";
        Response.AddHeader("content-disposition", attachment);

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