# Wednesday, February 11, 2009

Compressing DataSets

When passing datasets around via webservices we've had some problems with the size of the resulting xml. The following code will compress the dataset and turn it into a byte(). Note that you need to close the streams after calling WriteXml otherwise you'll get a "unexpected end of file" style exception when you try to load back into your dataset.

Dim ms As New IO.MemoryStream
Dim cs As New IO.Compression.GZipStream(ms, IO.Compression.CompressionMode.Compress, False)
results.WriteXml(cs)
cs.Close()
ms.Close()
Dim bytes As Byte() = ms.ToArray

Heres the corresponding decompression code for the server component.

Dim results As New DataSet
Dim ms As New IO.MemoryStream(_Args.Results)
Dim cs As New IO.Compression.GZipStream(ms, IO.Compression.CompressionMode.Decompress, False)
results.ReadXml(cs)
cs.Close()
ms.Close()

#    Comments [0] |
Comments are closed.