| 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 |
Print | posted @ Tuesday, October 18, 2005 3:52 PM