To decompress the data on the client, we can modify the MasterDetail project by adding a reference to the C1.Zip library (Silverlight version), then changing the svc_GetDataCompleted method in the MainPage.xaml.cs file as follows:
C# |
Copy Code
|
---|---|
void svc_GetDataCompleted(object sender, GetDataCompletedEventArgs e) { // Handle errors if (e.Error != null) { _tbStatus.Text = "Error downloading data..."; return; } // Parse data stream from server (DataSet as XML) _tbStatus.Text = string.Format( "Got data, {0:n0} kBytes", e.Result.Length / 1024); var ms = new MemoryStream(e.Result); _ds = new DataSet(); using (var zr = new C1.C1Zip.C1ZStreamReader(ms)) _ds.ReadXml(zr); //_ds.ReadXml(ms); // Got the data, bind controls to it BindData(); } |
Instead of reading the DataSet directly from the compressed stream, we now read it through a C1ZStreamReader that decompresses it automatically.
Note: Without compression, the application transfers about 150k bytes of data on startup. After making these small changes, the initial data transfer is reduced to less than 50k. These two small changes reduce the amount of data transferred by about two-thirds. This reduces network traffic and the time it takes for the application to initialize and show the data.