Geeks With Blogs
AzamSharp Some day I will know everything. I hope that day never comes.

Sometime ago I wrote an article about Exporting Datagrid to Excel, Word and Text Files. Sometimes we need to export Dataset to xml file. This is pretty simple as all you need is a literal control which will render the data to the output stream. Check out the code below:

  SqlConnection myConnection = new SqlConnection(ConnectionString);
        SqlDataAdapter ad = 
new SqlDataAdapter("SELECT * FROM Categories", myConnection);
        DataSet ds = 
new DataSet();
        ad.Fill(ds, "Categories");

        Literal l = 
new Literal();
        l.Text = ds.GetXml(); 

        
// Preparing to xml the file
        
Response.Clear();

        Response.AddHeader("content-disposition", "attachment;filename=FileName.xml");

        Response.Charset = "";

        Response.Cache.SetCacheability(HttpCacheability.NoCache);

        Response.ContentType = "application/vnd.xml";

        System.IO.StringWriter stringWrite = 
new System.IO.StringWriter();

        System.Web.UI.HtmlTextWriter htmlWrite = 
new HtmlTextWriter(stringWrite);

        l.RenderControl(htmlWrite);       
    


        
Response.Write(stringWrite.ToString());

        Response.End();

This will prompt a message asking that if you want to save the file or open it directly from its current location.

 

powered by IMHO

 

Posted on Tuesday, October 18, 2005 3:52 PM | Back to top


Comments on this post: Exporting DataSet to XML File

# re: Exporting DataSet to XML File
Requesting Gravatar...
The contents are good. But i would be helpful if some explanation is given
Left by Raja on Mar 20, 2006 8:07 AM

Your comment:
 (will show your gravatar)


Copyright © Mohammad Azam | Powered by: GeeksWithBlogs.net