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);
}
}
}
}
}
In another answer you mention you have not seen any examples of how to write directly from WriteXML to a stream. Here is how to stream directly to the file using your code as a base.
public void RetrieveData(Stream outputStream)
{
string connString = ConfigurationManager.ConnectionStrings["Platform"].ConnectionString;
using (SqlConnection conn = new SqlConnection(connString))
using (GZipStream compressStream = new GZipStream(outputStream, CompressionMode.Compress))
{
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = new SqlCommand("SELECT * FROM expenses.CST_COSTHEADER", conn);
adapter.Fill(dataset);
dataSet.WriteXml(compressStream);
}
}
For fun here is a version that is done as a extension method, you can just use it as dataset.WriteCompressedXml(stream)
public static class ExtensionMethods
{
public static void WriteCompressedXml(this DataSet dataset, Stream stream)
{
using (GZipStream compressStream = new GZipStream(stream, CompressionMode.Compress))
{
dataSet.WriteXml(compressStream);
}
}
public static void WriteCompressedXml(this DataSet dataset, Stream stream, XmlWriteMode mode)
{
using (GZipStream compressStream = new GZipStream(stream, CompressionMode.Compress))
{
dataSet.WriteXml(compressStream, mode);
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With