Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Status Updates

View All Updates

- - - - -

Tutorial: Visual Studio 2008 C# Serialization

visual studio 2008

  • Please log in to reply
3 replies to this topic

#1 Guest_Jordan_*

  • Guest

Posted 13 May 2008 - 02:00 PM


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.


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.

Posted Image

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.


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.

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);
     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;
Now lets test our code. First, enter some info on the four textboxes on the left.

Posted Image

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.

Posted Image

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.

Posted Image

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.

Post your questions or comments here. You can also create a new thread in the appropriate forum with your question.
  • 0

#2 Xav


    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8356 posts

Posted 14 May 2008 - 11:54 AM

Can you create a serializeable struct as well, or just a class?
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#3 ArekBulski


    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 480 posts

Posted 29 August 2009 - 03:41 AM

I am pretty sure structures can be serialized, too. I worked with XML serialization, and Binary serialization, too. They work with about anything of this sort. ;)

It is worth noting that certain classes, MemoryStream for example, do not serialize into XML properly. While at the same time Binary serialization will do just fine. The reason behind it is that XML serialization only "scratches the surface", works only with public properties.

Binary serialization touches everything, private and read-only fields as well. But if you modify your class later on then deserializing it back is probably a massacre.
  • 0

#4 leplep29


    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 10 January 2012 - 07:49 PM

can you create a sample tutorial for serializing and putting into database in SQL ? :thumbup:
  • 0

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download