Click here to Skip to main content
13,733,955 members
Click here to Skip to main content
Add your own
alternative version


14 bookmarked
Posted 25 Feb 2007
Licenced CPOL

Bob Custom XML Serializer

, 25 Feb 2007
Rate this:
Please Sign up or sign in to vote.
A custom XML serializer using .NET.


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?


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:

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:

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 

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

BeSerializedClass recursive_Class = new BeSerializedClass();
// Serialize BeSerializedClass into XML
XMLTransformerFactory xmlTransformerFactory = 
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).


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.

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


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.


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!


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


About the Author

Babak Ansari
Software Developer (Senior)
Canada Canada
Software development experience since 1993

- Programming Languages: C# .NET, Delphi, C++, Java and VB
- Development Methodologies (SDLC): RUP, Scrum and XP
- Database: SQL server, Oracle, Apollo database and MS Access.

Educations & Certificates
- Microsoft® Certified Professional (MCP)
- Sun® Certified Java2 Programmer
- B.S. in computer science

You may also be interested in...

Comments and Discussions

GeneralInteresting Pin
eclipse2k125-Feb-07 10:02
membereclipse2k125-Feb-07 10:02 
GeneralRe: Interesting Pin
Babak Ansari3-Apr-07 22:59
memberBabak Ansari3-Apr-07 22:59 
Generalimage Pin
h3225-Feb-07 5:30
memberh3225-Feb-07 5:30 
GeneralRe: image Pin
Babak Ansari3-Apr-07 22:46
memberBabak Ansari3-Apr-07 22:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.180920.1 | Last Updated 25 Feb 2007
Article Copyright 2007 by Babak Ansari
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid