XML Serialization in .NET


XML Serialization:

By using XML serialization only public properties and fields can be serialized.

If private members are to be serialized, other serialization methods should be used (Binary Serialization and SOAP Serialization).

It requires a parameterless constructor. This constructor is called during deserialization.


Serialized form of the object does not contain any type, assembly information of the class. Only the data is stored.

Using XML serialization not all the classes can be serialized. Classes that implement IDictionary cannot be serialized. E.g. Hashtables cannot be serialized. Arrays of objects can be easily serialized. Collections can be serialized but to serialize collections certain rules must be followed.

Null values are not serialized with XML serialization. To include the null members in the serialized form IsNullable attribute should be set to true.

For example: [ XmlElement( IsNullable = true ) ]


Here ClsSample is a Serializable class. It has private and Public members,

using System;
using System.Collections.Generic;
using System.Text;

namespace XMLSerilization
{
public class ClsSample
{
private int m_TotalCount = 5;
public String m_StrWord = "Ten";

public void ReAssignMemberValues()
{
m_TotalCount = 20;
m_StrWord = "Twenty";
}
public void DisplayMemberValueAfterDeSerialization()
{
Console.WriteLine("Private member value is " + m_TotalCount);
Console.WriteLine("Public member value is " + m_StrWord);
}
}
}

Following way you can Serialize and Deserialize the objects(through files)

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace XMLSerilization
{
public class ClsXML
{
public void ToXMLSerilization(Object objToXml, string filePath)
{
StreamWriter stWriter = null;
XmlSerializer xmlSerializer;
try
{
xmlSerializer = new XmlSerializer(objToXml.GetType());
stWriter = new StreamWriter(filePath);
xmlSerializer.Serialize(stWriter, objToXml);
}
catch (Exception exception)
{
throw exception;
}
finally
{
if (stWriter != null) stWriter.Close();
}
}

public object FromXMLSerilization(string filePath, Type type)
{
XmlSerializer xmlSerializer;
FileStream fileStream = null;
try
{
xmlSerializer = new XmlSerializer(type);
fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
object objectFromXml = xmlSerializer.Deserialize(fileStream);
return objectFromXml;
}
catch (Exception Ex)
{
throw Ex;
}
finally
{
if (fileStream != null) fileStream.Close();
}
}
}
}

//Main Class

using System;
using System.Collections.Generic;
using System.Text;

namespace XMLSerilization
{
public class Program
{
static void Main(string[] args)
{
ClsSample ObjSample;
ClsXML ObjXML;
ObjSample = new ClsSample();
ObjSample.ReAssignMemberValues();
ObjXML = new ClsXML();
//Serialize
ObjXML.ToXMLSerilization(ObjSample, "c:\\XMLSerialized.TXT");
ObjSample = null;
//Deserilize
ClsSample ObjDeseialXML;
ObjDeseialXML = (ClsSample) ObjXML.FromXMLSerilization("c:\\XMLSerialized.TXT", typeof(ClsSample)) ;
ObjDeseialXML.DisplayMemberValueAfterDeSerialization();
}
}
}

Output :
Private member value is 5
Public member value is Twenty

Actually reassigned value for the private member is 20 but you are unable to get that value in deserialized object. Because XML Serilization does not keep private members information

No comments: