Click here to Skip to main content
13,410,713 members (31,753 online)
Click here to Skip to main content
Add your own
alternative version


13 bookmarked
Posted 21 Jan 2011

Converting and Compressing a Dataset to Byte array

, 24 Jan 2011
Rate this:
Please Sign up or sign in to vote.
Convert a Dataset to bytes array and then compress that array.
Hello everybody,
Hope everyone is fine. This is my first article/tip on CodeProject. Pardon me for any mistakes or rule violations. Thanks.


There are a number of occasions when we have to pass datatable/Dataset from a web service to some other webservice or application. I will show you how to convert the dataset to an array of bytes and then compress that array so that the overall procedure becomes more efficient and optimized.


The namespace you will have to add in order to get this thing working are:

Convert and Compress Dataset to Bytes array

This function will take a dataset as argument and converts the dataset to a byte array and then compresses it.
Public Function CompressData(ByVal ds As DataSet) As Byte()
            Dim data As Byte()
            Dim memStream As New MemoryStream()
            Dim zipStream As GZipStream = New GZipStream(memStream, CompressionMode.Compress)
            ds.WriteXml(zipStream, XmlWriteMode.WriteSchema)
            data = memStream.ToArray()
            Return data
        Catch ex As Exception
            Return Nothing
        End Try
    End Function

Convert and Decompress Byte array to Dataset

Now the reciprocal of the above function. This function will decompress the bytes array received and converts it back to the dataset.

Public Function DecompressData(ByVal data As Byte()) As DataSet
            Dim memStream As New MemoryStream(data)
            Dim unzipStream As New GZipStream(memStream, CompressionMode.Decompress)
            Dim objDataSet As New DataSet()
            objDataSet.ReadXml(unzipStream, XmlReadMode.ReadSchema)
            Return objDataSet
        Catch ex As Exception
            Return Nothing
        End Try
    End Function


If you have to transfer/recieve Dataset/Datatable between webservices or web Applications, this would be a good idea to convert your dataset to a byte array and then compress the byte array. In this way, the whole procedure will be more optimized.

Hope this article was helpful.

Ahsan Sarfraz
Software Engineer


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

ahsan sarfraz
Engineer Netsolace
Pakistan Pakistan
Software Engineer.

You may also be interested in...

Comments and Discussions

GeneralReason for my vote of 5 thank you very much for the input, I... Pin
sgiaku29-Nov-11 4:56
membersgiaku29-Nov-11 4:56 
GeneralMaybe this will interest you - efficient implementation Pin
Espen Harlinn22-Jan-11 2:59
memberEspen Harlinn22-Jan-11 2:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180221.1 | Last Updated 24 Jan 2011
Article Copyright 2011 by ahsan sarfraz
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid