Introduction
This is a very short article simply to describe some useful serialization helper classes that I've used for quite a while and thought I would share. The classes allow single line calls to serialize and deserialize using either the XML or the WCF serializers. Nothing special or tricky - just saves some time!
Background
I use these classes primarily in the following scenarios:
- For unit testing serializable types to ensure that the classes are marked up correctly. I recently worked on a client site where a whole day had previously been lost due to a software defect caused by incorrectly marked up WCF
DataContract
classes. My solution was to add additional unit tests to "round trip serialize" all serializable types. This then uncovered some other similar errors in the code base. - For reading and writing application configuration at runtime. On occasions, I find the limitations of the .NET Framework configuration classes too restricting especially if I need to update configuration at runtime (e.g. saving user settings) and/or re-reading the config at runtime.
- When capturing serializable information for logging purposes
Usage
... With the XML Serializer
To serialize an instance of a serializable type (note use of type inference for a cleaner syntax):
XmlTestType instance = new XmlTestType();
string xml = XmlSerializationHelpers.XmlSerializeToString(instance);
... and to deserialize:
XmlTestType instance = XmlSerializationHelpers.XmlDeserializeFromString(xml);
... and to "round trip":
XmlTestType instance1 = new XmlTestType();
XmlTestType instance2 = XmlSerializationHelpers.XmlRoundtripSerialize(instance1);
... With the WCF Serializer
Very similar to the above, to serialize:
WcfTestType instance = new WcfTestType();
string xml = WcfSerializationHelpers.WcfSerializeToString<wcftesttype />(instance);
... and to deserialize:
WcfTestType instance = WcfSerializationHelpers.WcfDeserializeFromString<wcftesttype />(xml);
... and to "round trip":
WcfTestType instance1 = new WcfTestType();
WcfTestType instance2 = WcfSerializationHelpers.WcfRoundtripSerialize(instance1);
Solution, Classes and Unit Tests
The enclosed solution and unit tests contain the helper classes and associated unit tests to exercise these classes. The helper classes and method signatures are listed below:
WcfSerializationHelpers
-
public static string WcfSerializeToString<T>(T instance)
-
public static T WcfDeserializeFromString<T>(string xml)
-
public static bool IsWcfSerializable(object check)
-
public static T WcfRoundtripSerialize<T>(T instance) where T : class, new()
XmlSerializationHelpers
-
public static string XmlSerializeToString<T>(T instance)
-
public static T XmlDeserializeFromString<T>(string xml)
-
public static bool IsXmlSerializable(object check)
-
public static T XmlRoundtripSerialize<T>(T instance) where T : class, new()
Unit tests are defined in the WcfSerializationHelpersTests
and XmlSerializationHelpersTests
classes. The solution was developed in Visual Studio 2008 Team System but I've modified it to use NUnit (as this is more widely used) and also included a Visual Studio 2005 solution file for convenience if you need it.
License
No restrictions - do what you like with this code.
History
- Version 1.0 - 7th December, 2007 - Initial version