Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

WCF and XML Serialization Helper Classes

3.50/5 (6 votes)
11 Dec 2007Public Domain2 min read 1   454  
Helper classes to serialize and deserialize in a single line of code using XML/WCF serialization

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:

  1. 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.
  2. 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.
  3. 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):

C#
XmlTestType instance = new XmlTestType();
string xml = XmlSerializationHelpers.XmlSerializeToString(instance);

... and to deserialize:

C#
XmlTestType instance = XmlSerializationHelpers.XmlDeserializeFromString(xml);

... and to "round trip":

C#
XmlTestType instance1 = new XmlTestType();
XmlTestType instance2 = XmlSerializationHelpers.XmlRoundtripSerialize(instance1);

// compare instances etc...

// (see unit test project for example)

... With the WCF Serializer

Very similar to the above, to serialize:

C#
WcfTestType instance = new WcfTestType();
string xml = WcfSerializationHelpers.WcfSerializeToString<wcftesttype />(instance);

... and to deserialize:

C#
WcfTestType instance = WcfSerializationHelpers.WcfDeserializeFromString<wcftesttype />(xml);

... and to "round trip":

C#
WcfTestType instance1 = new WcfTestType();
WcfTestType instance2 = WcfSerializationHelpers.WcfRoundtripSerialize(instance1);

// compare instances etc...

// (see unit test project for example)

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

  • C#
    public static string WcfSerializeToString<T>(T instance)
  • C#
    public static T WcfDeserializeFromString<T>(string xml)
  • C#
    public static bool IsWcfSerializable(object check)
  • C#
    public static T WcfRoundtripSerialize<T>(T instance) where T : class, new()

XmlSerializationHelpers

  • C#
    public static string XmlSerializeToString<T>(T instance)
  • C#
    public static T XmlDeserializeFromString<T>(string xml)
  • C#
    public static bool IsXmlSerializable(object check)
  • C#
    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

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication