In another answer, you mention that you have not seen examples of how to write directly from WriteXML to a stream. Here's how to transfer a stream directly to a 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, there is a version that runs as an extension method, you can simply 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); } } }
source share