XML Serialization and DeSerialization using C#.

Here is the sample coding for a simple Serialization and DeSerialization program using C#.

myClass.CS

using System;
using System.Collections.Generic;
using System.Text;
namespace Serialization
{
public class myClass
{
protected string namefield;
protected string placefield;
public string name
{
get { return namefield; }
set { namefield = value; }
}
public string place
{
get { return placefield; }
set { placefield = value; }
}
}


button1_Click


myClass myClassObj = new myClass();
myClassObj.name = "Vinoth";
myClassObj.place = "Chennai";
XmlSerializer serializer = new XmlSerializer(typeof(myClass));
string path = Environment.CurrentDirectory + "\\serialization.xml";
TextWriter writer = new StreamWriter(path,true);
serializer.Serialize(writer, myClassObj);
writer.Close();

MessageBox.Show("Serialized");

FileStream fs = File.OpenRead(Environment.CurrentDirectory + "\\serialization.xml");
myClassObj=(myClass)serializer.Deserialize(fs);


MessageBox.Show(myClassObj.name);

That's it....

....S.VinothkumaR.

No comments: