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:
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
_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.
BeSerializedClass recursive_Class = new BeSerializedClass();
XMLTransformerFactory xmlTransformerFactory =
XMLTransformerFactory.CreateInstance();
string serializedXml = xmlTransformerFactory.ToXml(recursive_Class);
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.
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!