C# tutorial - Serialization


Serialization

Sometimes, you might want to store a collection of objects to a file and then read them back in your program. For example, a collection of objects that represents a group of students in a class have to be stored in a file. These objects will be used later in the program. Serialization is the process of writing an object to a file. In contrast, deserialization reads the object back from the file. An object that is serializable must be marked with Serializable keyword.

In C#, you can use the BinaryFormatter class (in System.Runtime.Serialization.Formatters.Binary namespace) to write an object to a file and read the object from the file. To write the object to the file, you will use its Serialize method. The Serialize method takes two arguments. One argument is the stream object representing the file that the object will be stored and another argument is the object. Its Deserialize method will read the object back from the file. It accepts one argument that is the stream object representing the file to be read from. In the example below, the Student class is marked with the Serializable keyword so that it can be serialized. The writeObject method can be called to write a student object to a file. The readObject method will read the object back.

[Serializable()]
class Student
{
private string id;
private string name;


public Student(string id, string name)
{
  this.id = id;
  this.name = name;

}

public string pid
{
  get { return id;}
  set { id=value;}
}
public string pname
{
  get { return name; }
  set { name = value; }
}
}


static void writeObject(string path)
{
FileStream fs = null;
ArrayList al = new ArrayList() ;
try
{
  fs = new FileStream(path, FileMode.Create, FileAccess.Write);
  BinaryFormatter bf = new BinaryFormatter();
  bf.Serialize(fs, new Student("999", "Dara"));

}
catch (Exception e) { Console.WriteLine(e.Message); }
finally
{
  fs.Close();

}
}

static void readObject(string path)
{
FileStream fs = null;
try
{
  fs = new FileStream(path, FileMode.Open, FileAccess.Read);
  BinaryFormatter bf = new BinaryFormatter();
  Student st =(Student) bf.Deserialize(fs);
  Console.WriteLine("Id={0}, Name={1}",st.pid,st.pname);

}
catch (Exception e) { Console.WriteLine(e.Message); }
finally
{
  fs.Close();

}
}

If you want to store a collection of many objects in a file, you need to package the objects in an ArrayList. Then write the ArrayList object to the file.

Example:

ArrayList al=new ArrayList();
fs = new FileStream(path, FileMode.Create, FileAccess.Write);
BinaryFormatter bf = new BinaryFormatter();
al.Add(new Student("0999", "Dara"));
al.Add(new Student("888", "Yuk"));

Simply, you can read the ArrayList object that contains the student objects as shown below:

ArrayList al = (ArrayList)bf.Deserialize(fs);

You can download the complete Serialization code from here.

 




Comments

MohitKumar comment

 MohitKumar

Check this for C# interview questions
http://skillgun.com/csharp/interview-questions-and-answers


2014-06-12
CAPTCHA image




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.