I have a simple application that retrieves a dataset from the database and converts it to an xml file. This xml file is then read in and compressed to a .gz file.
This seems pretty inefficient – is it possible to skip the step of writing to a temporary .xml file and reading it back into compress it? Can I automatically send the file to a stream which converts it directly to a converted xml format?
Here is my code:
public partial class _Default : System.Web.UI.Page
{
DataSet dataset = new DataSet();
string uri = "C:\\tester.xml";
string compressedfileuri = "C:\\tester.gz";
protected void Page_Load(object sender, EventArgs e)
{
//get the dataset
RetrieveData();
//serialize data to a xml file
dataset.WriteXml(uri);
//compress the data
Compress();
}
public void RetrieveData()
{
string connString = ConfigurationManager.ConnectionStrings["Platform"].ConnectionString;
using (SqlConnection conn = new SqlConnection(connString))
{
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = new SqlCommand("SELECT * FROM expenses.CST_COSTHEADER", conn);
adapter.Fill(dataset);
}
}
public void Compress()
{
using (FileStream fs = new FileStream(uri, FileMode.Open))
{
using (FileStream outFile = File.Create(compressedfileuri))
{
using (GZipStream Compress = new GZipStream(outFile, CompressionMode.Compress))
{
fs.CopyTo(Compress);
}
}
}
}
}
Why just not use following overload and write directly to ZIp stream?
DataSet.WriteXml Method (Stream, XmlWriteMode)