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";
        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 
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

COMMENT USING

PREMIUM SPONSORS

Hire Mobile & Web Developer on demand. 100% satisfaction. Try for 1 week or Money Back. Local and remote developers available all over USA.

Latest Blogs