Click here to Skip to main content
15,861,168 members
Articles / Mobile Apps / Windows Mobile
Article

Bob Custom XML Serializer

Rate me:
Please Sign up or sign in to vote.
4.00/5 (3 votes)
25 Feb 2007CPOL2 min read 41.7K   219   14   4
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)


Written By
Software Developer (Senior)
Canada Canada
Software development experience since 1993

Skills
- 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

Comments and Discussions

 
GeneralInteresting Pin
eclipse2k125-Feb-07 10:02
eclipse2k125-Feb-07 10:02 
GeneralRe: Interesting Pin
Babak Ansari3-Apr-07 22:59
Babak Ansari3-Apr-07 22:59 
Generalimage Pin
h3225-Feb-07 5:30
h3225-Feb-07 5:30 
GeneralRe: image Pin
Babak Ansari3-Apr-07 22:46
Babak 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.