Compressed Serialization

This tutorial shows how you can serialize objects in compressed files, and then load them back into the memory.

The sample creates a data table of people. The table is saved (serialized) into regular and compressed streams. Finally, the data is loaded back from either stream. Here is what the final application will look like:

 

Step 1: Create the main form.

Start a new Silverlight project in Visual Studio and from the Toolbox, add the following controls to the form:

      Five Button controls along the left edge of the form, as shown in the picture above. In the Properties window, make the following changes to each Button control:

Button

Button.Content Property

Button.Name Property

Button.IsEnabled Property

1

Create Data

btnCreate

True (Default)

2

Save Data

btnSave

True

3

Save Compressed Data

btnSaveCompressed

True

4

Load Data

btnLoad

True

5

Load Compressed Data

btnLoadCompressed

True

Note that the save and load buttons cannot be used until the data table has been created or saved.

      A DataGrid control on the right of the form.

Step 2: Add references and Imports/using statements.

Go to the Solution Explorer window and click the Show All Files button. Right-click on References, and select the Add Reference menu option. Select the C1.Silverlight.Zip assembly from the list, or browse to find the C1.Silverlight.Zip.dll file.

Select the MainPage.xaml.vb tab (MainPage.xaml.cs in C#) or go to View|Code to open the Code Editor. At the top of the file, add the following statements:

      Visual Basic

      C#

This declares the namespaces of the classes used in the project.

Step 3: Declare constants.

In the Code Editor of the page, type or copy the following lines in the body of the page implementation:

      C#

This constant defines the list of people that will appear in the grid.

Step 4: Add code to create the data table.

Add the following code to handle the Click event for the Create Data Table button:

      C#

The function uses standard ADO.NET objects and methods to create and populate a DataTable object, which is then bound to the DataGrid control.

Step 5: Add code to save the data table.

Add the following code to handle the Click event for the Save Data Table button:

      C#

The first set of code serializes the DataTable into a regular file, and the second serializes the DataTable into a compressed file. Note that only one additional line is required to compress the data.

In both cases, the serialization is executed by the BinaryFormatter object. The only difference is that in the first case, the Serialize method is called with a regular file stream as a parameter; in the second, a C1ZStreamWriter is used instead.

Step 6: Add code to save the compressed data.

Add the following code to handle the Click event for the Save Compressed Data button:

      C#

Step 7: Add code to load the data table from the regular file.

Add the following code to handle the Click event for the Load Data Table button:

      C#

The first main line of code creates a new BinaryFormatter object and the second one calls its Deserialize method. The Deserialize method takes a single parameter: the stream in which the object is defined. In this case, the stream is a regular file stream.

Step 8: Add code to load the data table from the compressed file.

Add the following code to handle the Click event for the Load Compressed Data Table button:

      C#

The main lines are similar to the code used to deserialize data from a regular file. The only difference is that instead of passing a regular file stream to the Deserialize method, we now use a C1ZStreamReader object.

This concludes the Compressed Serialization tutorial.

 

 


Send us comments about this topic.
Copyright © GrapeCity, inc. All rights reserved.