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;
4.
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);
fs.Close();
Console.WriteLine("Successfully wrote text to file press any key to Read the text from the file");
Console.ReadKey();
Console.Clear();
FileStream fs2 = new FileStream(@"C:\Test.Sam", FileMode.Open);
BinaryFormatter bf2 = new BinaryFormatter();
string s2 = (string)bf2.Deserialize(fs2);
fs2.Close();
Console.WriteLine("Below is the text written to the file");
Console.WriteLine(s2);
Console.ReadKey();
}
}
}
Free MCTS EXAM training tutorials on this website, soon will be launching video tutorials for all the same topics
No comments:
Post a Comment