C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » .NET Framework »

Serialization in C#


Posted Date: 29 May 2004    Resource Type: Articles    Category: .NET Framework
Author: Rakesh Chander SharmaMember Level: Silver    
Rating: 1 out of 5Points: 5



Serialization is the process of writing the state of an object to a byte stream. This is useful when we want to save the state of our program to a persistent storage area, such as a file. At a later time we can restore these objects by using the process of deserialization.

As a simplest example of this we created an array of objects of class emp. We stored this data in a .dat file on the secondary storage and retrieved it using deserialization. Here is how the program will look

using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace serialize
{
[Serializable]
public class emp
{
int id;
string name ;
double salary ;
public emp ( int i, string n, double s)
{
id = i ;
name = n ;
salary = s ;
}

public string ToString()
{
return id + " " + name + " " + salary ;
}
}

public class Class1
{
public static int Main ( string[] args )
{
emp[] e =
{
new emp ( 1, "Rahul", 4500 ),
new emp ( 2, "Janaki", 5345 ),
new emp ( 3, "Amol", 4460 ),
new emp ( 4, "Anil", 9456 ),
new emp ( 5, "Srikanth", 9500 ),
new emp ( 6, "Ravi", 5670 ),
new emp ( 7, "Abhijeet", 3345 ),
new emp ( 8, "prashant", 8500 )
} ;

File f = new File ( "C:\\emp.dat" ) ;
Stream s = f.Open ( FileMode.Create ) ;
BinaryFormatter b = new BinaryFormatter() ;

for ( int i = 0 ; i < e.Length ; i++ )
{
b.Serialize ( s, e[i] ) ;
}

s.Close();

s = f.Open(FileMode.Open);
for ( int i = 0 ; i < 8 ; i++)
{
emp ee = (emp)b.Deserialize(s);
Console.WriteLine ( ee.ToString() ) ;
}

s.Close();
return 0;
}
}
}
[/CODE}

The Serializable attribute indicates that a class can be serialized. Here we intend to serialize the emp class so we should write it above the class definition. We created a new file emp.dat in the C drive and dumped the emp objects in it. Next we opened the same file and used the information for displaying. The BinaryFormatter class provides a way to serialize and deserialize an object, or an entire graph of connected objects; it uses a binary format for the serialized stream, which is both very compact, and fast to parse.




Responses

Author: Murali M Nagendranath    18 Jun 2004Member Level: Bronze   Points : 0
Hey Rakesh,

I think the code had compilation errors.
I changed it to the following and it works fine.

using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace Serialization
{
[Serializable]
public class emp
{
int id;
string name;
double salary;

public emp(int i, string n, double s)
{
id = i;
name = n;
salary = s;
}

public override string ToString()
{
return id + " "+ name + " " +salary;
}

}//End of emp

public class class1
{
public static int Main(string[] args)
{
emp[] e = { new emp(1,"Mark",5000), new emp(2,"Mich",6000),new emp ( 3, "Amol", 4460 ),
new emp ( 4, "Anil", 9456 ),
new emp ( 5, "Srikanth", 9500 ),
new emp ( 6, "Ravi", 5670 ),
new emp ( 7, "Abhijeet", 3345 ),
new emp ( 8, "prashant", 8500 )
} ;


//File f = new File ("c:\\emp.dat");
Stream s = File.Open ("c:\\emp.txt",FileMode.Create,FileAccess.ReadWrite);
//Stream s = f.Open( FileMode.Create ) ;
BinaryFormatter b = new BinaryFormatter ();

for ( int i=0; i < e.Length; i++)
{
b.Serialize (s,(object)e[i]);
}

s.Close();

s = File.Open("c:\\emp.txt",FileMode.Open,FileAccess.ReadWrite);
for ( int i=0;i<8;i++)
{
emp ee = (emp)b.Deserialize(s);
Console.WriteLine (ee.ToString ());

}
s.Close();
Console.ReadLine();
return 0;




}



}

}



Author: santosh narayan poojari    26 Nov 2004Member Level: Gold   Points : 0
Hi!
Can u please explain me why we wrote 'Override' in method of class.i m novice to c#;i have basic idea of oops.
Can u explain below declaration.i understood what it does.I want to know its conceptual defination.

emp ee = (emp)b.Deserialize(s);
Thanks ,
Happy coding..



Author: Pankaj Singla    05 Jan 2006Member Level: Bronze   Points : 0
we should write the below statement
emp ee = (emp)b.Deserialize(s);

as
emp[] ee = (emp[])b.Deserialize(s);

This time we are going to deserialize the array.

--Pankaj Gupta


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Windows Management Instrumentation
Previous Resource: Redirecting the Standard Streams
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use