INTRODUCTION
Serialization is the process of persisting an object to disk. Another application can deserialize your object and it will be in the same state it was before the serialization. This tutorial talks about XML serialization. The namespace containing classes and methods suitable for such a serialization is System.Xml.Serialization.
XML SERIALIZATION
In order to serialize an object you must first create an XmlSerializer object. You must also create a stream that will write to or read from a file. Then, you call the appropriate method of serialization passing it the stream object you created. To deserialize an xml serialized object you created you simply call the deserialize method passing it the stream that reads from the xml document. The final step is to cast the object into the correct type.
When you need to exchange information with an application that is not based on the .Net framework you should use Xml serialization. Xml provides the following benefits over standar serialization techniques:
1. Greater interoperability: Xml is a text file based format and all modern operating systems and developing environments include libraries for processing such files.
2. Administrator friendly: by storing objects in xml format , it gives the administrators the opportunity to view and edit xml files. So, an administrator can easily modify your object or troubleshoot problems.
3. Better forward compatibility: xml-serialized objects are self-described. When you need to replace your application with a newer one, the transition will be straight forward.
SERIALIZE AND DESERIALIZE DATA
The basic steps for creating an Xml serialization are the following ones:
· Create a stream, Textwriter or XmlWriter object to store the serialized data.
· Create an XmlSerializer object. You need to pass the type of object you want to serialize.
· Call the XmlSerializer.Serialize() method to serialize the object. The produced data is extracted to the stream.
The following console application demonstrates these concepts. An object of DateTime is being serialized by storing its data to an xml file. Then, after some time, the user presses a key and the application retrieves the stored data. The information on the file is printed on the screen.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
//Create file to save the data to
FileStream myStream = new FileStream("Output.XML", FileMode.Create);
// Create an XML serializer object
XmlSerializer myXs = new XmlSerializer(typeof(DateTime));
//Serialize the data
myXs.Serialize(myStream, System.DateTime.Now);
myStream.Close();
Console.WriteLine("Press a key to continue");
Console.ReadLine();
Console.WriteLine("The time is " + DateTime.Now.TimeOfDay.ToString());
//DESERIALIZE THE DATA
FileStream mySecondFS = new FileStream("Output.XML", FileMode.Open);
XmlSerializer mySecondXs = new XmlSerializer(typeof(DateTime));
DateTime StoredTime = (DateTime)mySecondXs.Deserialize(mySecondFS);
mySecondFS.Close();
Console.WriteLine("The time was " + StoredTime.TimeOfDay.ToString());
Console.ReadKey();
}
}
}
Trackback(0)
 |