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.
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 
Pankaj Pandey
Pankaj Pandey

Hello Friend,This is Pankaj Pandey, Software Developer on .net Platform.I have knowledge of  Development in C#,ASP.net as well as Sql server,and worked on Dot net 1.1,2.0,3.5,4.0 and sql server 2005,2008.Also worked... Read more