Lost Password?

  #1 (permalink)  
Old 05-13-2008, 05:00 PM
Jordan's Avatar   
Jordan Jordan is offline
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Age: 26
Posts: 4,856
Last Blog:
Zend Studio for Eclips...
Rep Power: 20
Jordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud of
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan
Default Tutorial: Visual Studio 2008 C# Serialization

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:

Code:
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.

Code:
[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:

Code:
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.

Code:
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.
__________________
CodeCall Blog | CodeCall Wiki | Shareware Site | Linux Forum | Write a Blog
Don't hesitate to ask any questions that you have! Check out our ASCII Calculator!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 05-14-2008, 02:54 PM
Xav's Avatar   
Xav Xav is offline
Code Warrior
 
Join Date: Mar 2008
Location: London, England
Posts: 3,471
Last Blog:
Web slideshow in JavaS...
Rep Power: 30
Xav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to all
Send a message via MSN to Xav
Default Re: Tutorial: Visual Studio 2008 C# Serialization

Can you create a serializeable struct as well, or just a class?
__________________
Xav, the power of youth
Worship the Creator... not his creations
Web Site | Beta Site
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Tags
c# serialization, c# tutorials



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Tutorial: Visual Studio 2008 C# Compressing Jordan CSharp Tutorials 0 05-13-2008 04:52 PM
Visual Studio 2008: C# Hello World Tutorial Jordan CSharp Tutorials 1 02-27-2008 07:48 AM
Tutorial: Visual Studio 2008 Obfuscating with Dotfuscator Jordan Tutorials, Classes and Code 0 02-08-2008 02:01 PM


All times are GMT -5. The time now is 10:37 AM.

Contest Stats

Xav ........ 164.00000
dargueta ........ 128.00000
John ........ 127.00000
gaylo565 ........ 18.00000
XaNaX ........ 15.00000
Johnnyboy ........ 3.00000
navghost ........ 1.00000

Contest Rules

Ads