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#
using System.IO;
using System.Data.OleDb;
using System.Runtime.Serialization.Formatters.Binary;
using C1.C1Zip;
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#
public partial class MainPage : PhoneApplicationPage
{
PersonList personList = new PersonList();
private int idx = -1;
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#
private void button1_Click(object sender, RoutedEventArgs e)
{
for (int i = 0; i < 1000; i++)
{
personList.Persons.Add(new Person()
{
FirstName = string.Format("First Name {0}", i),
LastName = string.Format("Last Name {0}", i),
Age = i,
City = string.Format("City {0}", i)
});
}
ShowData(personList.Persons[0]);
idx = 0;
}
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#
private void button2_Click(object sender, RoutedEventArgs e)
{
IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();
var stream = isf.CreateFile("persons.tmp");
C1ZStreamWriter compressor = new C1ZStreamWriter(stream);
DataContractSerializer dcs = new DataContractSerializer(typeof(PersonList));
dcs.WriteObject(compressor, personList);
stream.Close();
}
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#
private void button3_Click(object sender, RoutedEventArgs e)
{
IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();
var stream = isf.OpenFile("persons.tmp", FileMode.Open);
DataContractSerializer dcs = new DataContractSerializer(typeof(PersonList));
C1ZStreamReader compressor = new C1ZStreamReader(stream);
personList = (PersonList)dcs.ReadObject(compressor);
stream.Close();
}
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#
private void button4_Click(object sender, RoutedEventArgs e)
{
if (--idx < 0)
{
idx = 0;
}
this.ShowData(personList.Persons[idx]);
}
private void button5_Click(object sender, RoutedEventArgs e)
{
if (++idx >= personList.Persons.Count)
{
idx = personList.Persons.Count - 1;
}
this.ShowData(personList.Persons[idx]);
}
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.
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Runtime.Serialization;
using System.Collections.Generic;
namespace CompressedSerializationTest
{
[DataContract]
public class Person
{
[DataMember]
public string FirstName
{
get;
set;
}
[DataMember]
public string LastName
{
get;
set;
}
[DataMember]
public int Age
{
get;
set;
}
[DataMember]
public string City
{
get;
set;
}
}
[DataContract]
public class PersonList
{
private List<Person> _persons = new List<Person>();
[DataMember]
public List<Person> Persons
{
get
{
return this._persons;
}
set
{
this._persons = value;
}
}
}
}
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.