Search this blog

You can search for all the topics in our blog using our google custom search now.

Search the blog


Sunday, May 23, 2010

How to serialize and de-serialize simple objects Part 1

1. Serialization is the process of converting objects into linear sequence of bytes that can be stored or transferred.

2. You got to include all these namespaces to use serialization, they are:-

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

3. System.IO namespace for working with all available streams and serialization,de-serialization is basically done by BinaryFormatter Objects which is available in System.Runtime.Serialization.Formatters.Binary;


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace serilizationexample

class Program

static void Main(string[] args)
Console.WriteLine("Please enter the text to write to the file");
string s1 = Console.ReadLine();
FileStream fs = new FileStream(@"C:\Test.Sam", FileMode.Create);
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs, s1);
Console.WriteLine("Successfully wrote text to file press any key to Read the text from the file");

FileStream fs2 = new FileStream(@"C:\Test.Sam", FileMode.Open);
BinaryFormatter bf2 = new BinaryFormatter();
string s2 = (string)bf2.Deserialize(fs2);

Console.WriteLine("Below is the text written to the file");


Free MCTS EXAM training tutorials on this website, soon will be launching video tutorials for all the same topics

No comments:

Post a Comment