C# SERIALIZATION TECHNIQUES
When programming with object oriented languages you may need to transfer data through a network, or between applications. The .Net environment gives you the opportunity to accomplish this task without too much hassle. Suppose you have an object and you must pass it to another application. How do you do that? You only need to serialize the object either in binary or in xml format and your object is ready to be transferred. In this tutorial we will see an example of such a serialization in a binary format. We will create an object in a custom serializable class and then we will serialize it in a binary file. We then deserialize the file and extract its parameters so as to verify that they have been transferred correctly.
2. GRAPHICAL USER INTERFACE
Create a new visual C# project named Serialization. Create a form as the one shown in picture 1. Notice that the textboxes on the right side are disabled.

The user will first enter the details in the corresponding textboxes on the left. Then through the serialization technique the textboxes information will be passed to the textboxes on the right.
3. CODE DEVELOPMENT
We first start with adding the necessary “Using..” statements in our project to include the appropriate namespaces:
using System.IO; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary;The first statement is necessery for the use of FileStream objects whereas the second and third allows us to use the binary formatters and methods of serialization/ deserialization technique. We will define a class to be our object that will be serialized to a binary file, and then deserialized ( to another object this time) to obtain its original contents. The serialized data go directly to the binary file and the deserialized data from the binary file go directly to our second object. It is like transporting data through the network, only in this case we use our local hard drive. For a class to be serializable we must add the serializable attribute to it. The next statements show the definition of a serializable class named Person with three public parameters.
[Serializable] private class Person { public string Name, Surname, Title, FathName; }We will create two objects of this class. The one will take the values of the textboxes of our form. We will Serialize it to a pre-specified location on the hard drive, deserialize it into the second object and check to see if it matches the first object by extracting its parameters to the textboxes on the right. For the serialization we also use a FileStream object for writing the binary data and a BinaryFormatter object for the serialization of the data. The following code demonstrates the whole serialization procedure.Add it to the button1 click event:
private void button1_Click(object sender, EventArgs e) { button1.Enabled = false; Person Bob = new Person(); Bob.Name = textBox1.Text; Bob.Surname = textBox2.Text; Bob.Title = textBox3.Text; Bob.FathName = textBox4.Text; FileStream filestream = new FileStream(@"C:\output11.txt", FileMode.Create); BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(filestream, Bob); filestream.Close(); button1.Enabled = true; }Now the procedure to deserialize the object is somewhat similar and straight-forward. With the press of the second button a new “Person” object is created and it gets the deserialized data from the binary file.
private void button2_Click(object sender, EventArgs e) { FileStream filestream2 = new FileStream(@"C:\output11.txt", FileMode.Open); BinaryFormatter bf2 = new BinaryFormatter(); Person JohnDoe = new Person(); JohnDoe = (Person)bf2.Deserialize(filestream2); textBox5.Text = JohnDoe.Name; textBox6.Text = JohnDoe.Surname; textBox7.Text = JohnDoe.Title; textBox8.Text = JohnDoe.FathName; filestream2.Close(); }Now lets test our code. First, enter some info on the four textboxes on the left.

Go to C:\Output11.txt and open the file. You will not see a normal notepad file beacause the data is in binary and not text format. Instead, you should see something like the data in Picture3.

Press the deserialize button to open the binary file,deserialize it and read its contents to the righ side textboxes. The output of he second object is the same as the input ( first object) as it can be seen from picture4.

Binary formatted objects can only be read by .Net Framework-based applications. You can also serialize with an XML based formatter, the so-called SoapFormatter. For a transmission across a network the objects created with this formatter are less likely to be rejected from a firewall. Finally, .Net gives you the opportunity to serialize directly to XML format but it is beyond the scope of this tutorial.
Questions/Comments?
Post your questions or comments here. You can also create a new thread in the appropriate forum with your question.