Monday, March 21, 2011

Understanding the Process of Serialization in console application.

The Stream Reader, Stream Writer, Binary Reader, and Binary Writer classes in the Microsoft .NET Framework class library enable you to build applications that can read and write text and primitive data types by using streams. However, in many cases, you must also read and write data structures that your own classes and structures define. Additionally, you may want to emit this data in a different format from the default encoding that these classes implement. For example, a common requirement is to read and write data as an XML stream. The process of converting objects into a format that can be written to a stream is called serialization. The converse process, reading data from a stream and converting the data into a set of objects, is called deserialization.
Serialization: The process of converting an object into a series of bytes.
Runtime serialization:
Binary Formatter and Soap Formatter classes control the serialization format:
Both implement the IFormatter interface.
Objects being serialized:
Must be instances of Serializable classes.
Can implement the ISerializable interface.

Serialization Formatters in the .NET Framework
The format of the byte stream that the serialization process emits is governed by a formatter object. When you serialize data, you construct a formatter object that implements the appropriate format. The .NET Framework class library supplies two formatters: the Binary Formatter class and the Soap Formatter class.
The Binary Formatter class is located in the
System.Runtime.Serialization.Formatters.Binary namespace. You can use this class to
Serialize data by using the internal binary format that the .NET Framework understands.

Note: The Binary Formatter class is implemented in the System assembly and is automatically available to a .NET Framework application. The Soap Formatter class is implemented in the System.Runtime.Serialization.Soap assembly, and you must add a reference to this assembly in your application to use the Soap Formatter class.

Example of Binary Formatter:

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

namespace mod6_less1
class Program
static void Main(string[] args)
myclass m = new myclass();
Console.WriteLine("Enter the course Name:");
m.Cname = Console.ReadLine();
Console.WriteLine("Enter the course fee:");
m.Cfee = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the course duration ");
m.duration = Convert.ToInt32(Console.ReadLine());

Console.WriteLine(m.Cfee );

//Binary Serialization
using (FileStream filestream = new FileStream(@"D:\\binfile.txt", FileMode.OpenOrCreate, FileAccess.Write))
BinaryFormatter formater = new BinaryFormatter();
formater.Serialize(filestream, m);

using (FileStream filestream = new FileStream(@"D:\\binfile.txt", FileMode.OpenOrCreate, FileAccess.Read))
BinaryFormatter formater = new BinaryFormatter();
m = (myclass)formater.Deserialize(filestream);


//m.Cname = ".Net";
//m.Cfee = 18000;
//m.duration = 4;

[Serializable ]
public class myclass
public string Cname;
public int Cfee;
public int duration;

No comments:

Recent Posts