Thursday, June 7, 2007

.Net Serialization

There are 2 methods of serialization provided in the .net.
1. Using XMLSerializer class - namespace : System.XML.Serialization
2. Using SoapFormatter/BinaryFormatter class - namespace : System.Runtime.Serialization.Formatter.Soap/Binary

XMLSerializer class is used primarily for web services because its good for cross plateform serialization.But it has its limitations, like class must have parameterless constructor, it cant serialize private class fields and only Read/Write properties can be serialized. A class needs to be declared public. We can choose not to serialise class fields by attributing them with [XMLIgnore] attribute. Example-


[Serializable()]
public class TestSimpleObject
{
int member1;
string member2;
string member3;
double member4;
// A field that is not serialized.
[NonSerialized()]
public string member5;
public TestSimpleObject()
{
member1 = 11;
member2 = "hello";
member3 = "world";
member4 = 3.14159265;
member5 = "hello world!";
}
}
public void Print()
{
Console.WriteLine("member1 = '{0}'", member1);
Console.WriteLine("member2 = '{0}'", member2);
Console.WriteLine("member3 = '{0}'", member3);
Console.WriteLine("member4 = '{0}'", member4);
Console.WriteLine("member5 = '{0}'", member5);
}
}


void XmlSerializationFn()
{
//Creates a new TestSimpleObject object.
TestSimpleObject obj = new TestSimpleObject();
Console.WriteLine("Before serialization the object contains: ");
obj.Print();
//Opens a file and serializes the object into it in binary format.
Stream stream = File.Open("Xmldata.xml", FileMode.Create);
XmlSerializer Xml = new XmlSerializer(typeof(TestSimpleObject));
Xml.Serialize(stream, obj);
stream.Close();
//Empties obj.
obj = null;
//Opens file "Xmldata.xml" and deserializes the object from it.
stream = File.Open("Xmldata.xml", FileMode.Open);
Xml = new XmlSerializer(typeof(TestSimpleObject));
obj = (TestSimpleObject)Xml.Deserialize(stream);
stream.Close();
Console.WriteLine("");
Console.WriteLine("After deserialization the object contains: ");
obj.Print();
}

SoapFormatter/BinaryFormatter class is used primarily for Remoting. BinaryFormatter should be used if serialization/Deserialization performed on .net plateform and SoapFromatter in all other cases. It imposes less restriction than XMLSerializer.Target class does not require to have parameterless constructor and Private/Public fields of the class can be serialized. But it also has a limitation - the constructor of the new object not invoked at the time of deserialization A class needs to be attributed [serializeable]. We can choose not to serialise class fields by attributing them with [NonSerialized] attribute. Example-


void SoapFormatterFn()
{
//Creates a new TestSimpleObject object.
TestSimpleObject obj = new TestSimpleObject();
Console.WriteLine("Before serialization the object contains: ");
obj.Print();
//Opens a file and serializes the object into it in binary format.
Stream stream = File.Open("Soapdata.xml", FileMode.Create);
SoapFormatter formatter = new SoapFormatter();
formatter.Serialize(stream, obj);
stream.Close();
//Empties obj.
obj = null;
//Opens file "Soapdata.xml" and deserializes the object from it.
stream = File.Open("Soapdata.xml", FileMode.Open);
formatter = new SoapFormatter();
obj = (TestSimpleObject)formatter.Deserialize(stream);
stream.Close();
Console.WriteLine("");
Console.WriteLine("After deserialization the object contains: ");
obj.Print();
}
void BinaryFormatterFn()
{
//Creates a new TestSimpleObject object.
TestSimpleObject obj = new TestSimpleObject();
Console.WriteLine("Before serialization the object contains: ");
obj.Print();
//Opens a file and serializes the object into it in binary format.
Stream stream = File.Open("Binarydata.xml", FileMode.Create);
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, obj);
stream.Close();
//Empties obj.
obj = null;
//Opens file "Binarydata.xml" and deserializes the object from it.
stream = File.Open("Binarydata.xml", FileMode.Open);
formatter = new BinaryFormatter();
obj = (TestSimpleObject)formatter.Deserialize(stream);
stream.Close();
Console.WriteLine("");
Console.WriteLine("After deserialization the object contains: ");
obj.Print();
}

No comments: