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 using the NorthWind database. 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 Windows Phone project in Visual Studio and from the Toolbox, add the following controls to the form:

      Five Button controls and eight TextBlock controls. In the Properties window, make the following changes to each control:

Button

Button.Content Property

Button.Name Property

1

Create Data

button1

2

Save Compressed Data

button2

3

Load Compressed Data

button3

4

Previous

button4

5

Next

button5

 

TextBlock

Button.Content Property

Button.Name Property

1

First Name:

textBlock1

2

Last Name:

textBlock2

3

Age:

textBlock3

4

City:

textBlock4

5

""

tbFirstName

6

""

tbLastName

7

""

tbAge

8

""

tbCity

 

Step 2: Add references and Imports 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.Phone.Zip assembly from the list, or browse to find the C1.Phone.Zip.dll file.

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

      C#

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

Step 3: Declare constants.

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

      C#

These constants define the name of the database used to populate the data table and the file names used to serialize the data.

Step 4: Add code to show data.

1.   Double-click the Create Data button to create the button1_Clickhandler.

2.   Add the following code to handle the Click event:

      C#

 

Step 5: Add code to save the data table.

1.   Double-click the Save Compressed Data button to create thebutton2_Click handler.

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

      C#

The code serializes the data into compressed file. In this case,  a C1ZStreamWriter is used to compress the data.

Step 6: Add code to load the compressed data.

1.   Double-click the Load Compressed Data button to create thebutton3_Click handler.

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

      C#

Step 7: Add code for the Previous and Next Buttons.

1.   Double-click the Previous button to create the button4_Click handler.

2.   Add the following code to handle the Click event for the button4_Click(Previous) button.

      C#

 

Step 8: Add a data class to populate the TextBlocks.

In this step you will add a Data class that will add people to a list that can be viewed in the TextBlock controls.

1.   Right-click the project in the Solution Explorer and select Add | New Item.

2.   Select Class, name it Data, and click Add.

3.   Add the following code to the Data.cs file. Note that you must have a reference to the Sytem.Runtime.Serialization.dll in your project as well.

      Code to add

 

Run the project and tap the Create Data button. Click the Next button to scroll through the people in the list. Now you can save and load the compressed data.

 

 

This concludes the Compressed Serialization tutorial.


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