Wednesday, May 2, 2012

Saving XML files from a dataSet through ASP.NET

Currently I have two code samples, once takes a table from my database, puts it into a DataSet and then saves the DataSet and an XML file to my desktop.



The other code saves a file from a webpage. What I would like to do is combine these two code examples so that I can save my XML file through the internet.



Here are the samples:



SqlDataAdapter adapter = new SqlDataAdapter(query, connectionString);
DataSet questionSet = new DataSet();
adapter.Fill(questionSet, "Questions");

questionSet.WriteXml("../../../../../Users/User/Desktop/MyTest");


This successfully saves my table as an XML document and saves it to my Desktop.



The code sample from the MSDN for saving files through ASP.NET



System.String filename = "myFile.txt";

Response.ContentType = "text/xml";

// initialize the http content-disposition header to
// indicate a file attachment with the default filename
// "myFile.txt"
System.String disHeader = "Attachment; Filename=\"" + filename +
"\"";
Response.AppendHeader("Content-Disposition", disHeader);

// transfer the file byte-by-byte to the response object
System.IO.FileInfo fileToDownload = new
System.IO.FileInfo("C:\\downloadJSP\\DownloadConv\\myFile.txt");
Response.Flush();
Response.WriteFile(fileToDownload.FullName);}


This successfully saves a file through my browser.



As far as I understand it, it seems that .WriteXml creates a the file from the dataSet. Is there a way to throw that file at the other code and tell the browser to download the .WriteXml file instead?



All of this code looks almost like magic to me, but then again, it's .NET, which is known for its pixie dust!





No comments:

Post a Comment