Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Mobile / WinMobile

Bob Custom XML Serializer

4.00/5 (3 votes)
25 Feb 2007CPOL2 min read 1   219  
A custom XML serializer using .NET.

Introduction

Have you ever thought of having an XML serializer that can be programmed to serialize certain fields in an object? Imagine a scenario where you want to serialize an object with a password field and this field should be encrypted using another key field in the same object. That means, do some custom processing before serialization. What if you should serialize private fields of an object in XML format, or set a custom name for XML tags?

Solution

The idea is to implement an interface for the object to be serialized, with one method that is called on the serialization and the de-serialization process. The objects that are involved in the serialization process should implement the following interface:

C#
public interface IXMLTransferable
{
    void Transformer IXMLTransformer( xmlTransformer );
}

Inside the above Transformer method, use the xmlTransformer variable that is being passed to the method at the time of serialization and specify the fields involved in the serialization using the AddField method. For example:

C#
class BeSerializedClass : IXMLTransferable
{
    private int xVar; 
    private BeSerializedClass _recursive_composition;
public int XVar {
    get { return xVar; }
    set { xVar = value; }
}
public Recursive_Class Recursive_Class_Object
{
    get { return _recursive_composition; }
    set { _recursive_composition = value; }
}
#region IXMLTransferable Transformer Member
public void Transformer(IXMLTransformer xmlTransformer)
{
    xmlTransformer.AddField<int>("xVar", ref xVar);
    xmlTransformer.AddField<BeSerializedClass>("_recursive_composition", ref 
    _recursive_composition);
}
#endregion
}

At the time of serialization, use an instance of the xmlTransformerFactory class to serialize/de-serialize the object into/from an XML string.

C#
BeSerializedClass recursive_Class = new BeSerializedClass();
// Serialize BeSerializedClass into XML
XMLTransformerFactory xmlTransformerFactory = 
                      XMLTransformerFactory.CreateInstance();
string serializedXml = xmlTransformerFactory.ToXml(recursive_Class);
// Deserialize subjectClass from XML
Recursive_Class subjectClass_Deserialized = 
  xmlTransformerFactory.FromXml<Recursive_Class> (serializedXml);

The Transformer method of the IXMLTransferable interface is called when the object is serialized (ToXml) and deserialized (FromXml).

Design

There is a factory object called XMLTransformerFactory to serialize/deserialize an IXMLTransferable object, this factory creates a SerializerXMLTransformer object to serialize the object and a DeserializerXMLTransformer object to deserialize the object. Both classes are a type of the AbstractXMLTransformer class that implements the IXMLTransformer interface. When an IXMLTransferable object is being serialized, its Transformer method is called, passing the IXMLTransformer Serializer object as its variable to add the fields of the class to the XML file.

C#
internal abstract class AbstractXMLTransformer : IXMLTransformer
internal class SerializerXMLTransformer : AbstractXMLTransformer
internal class DeserializerXMLTransformer : AbstractXMLTransformer

Implementation

The code for the XML Serializer is in a library (DLL) called BOB.XMLSerializer. There are various scenario based unit tests that are provided as samples.

Deployment

NUnit is required to run the test (BOB.XMLSerializer.Tests) project. Give the path of NUnit (.\Program Files\NUnit-Net-2.0 2.2.8\bin\nunit-gui.exe) to the project.

Thank you!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)