Wednesday, October 26, 2005

Serialization in C Sharp

It was never easier to serialize and deserialize an object in c sharp here
is an example code.

using System.Xml.Serialization;

public void Serialize()

{

XmlSerializer serial= new XmlSerializer(typeof(T));

System.IO.StreamWriter sw= new System.IO.StreamWriter("mapa.xml");

serial.Serialize(sw,this);

}

public static LoadInstance(string fileName)

{

XmlSerializer serial= new XmlSerializer(typeof(T));

System.IO.StreamReader sw= new System.IO.StreamReader(fileName);

return()serial.Deserialize(sw);

}

Where T must be replace by the class you want to serialize.

Does this looks familiar? This code is almost what you have to do
in the .net Framework 2.0. I will publish a sample using the 2.0 framework soon.

No comments: