Click here to Skip to main content
15,921,779 members
Articles / Programming Languages / C#
Article

WCF and XML Serialization Helper Classes

Rate me:
Please Sign up or sign in to vote.
3.50/5 (6 votes)
11 Dec 2007Public Domain2 min read 51.3K   453   24   8
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


Written By
Architect oo.magic
United Kingdom United Kingdom
Mark Gwilliam is a freelance .Net consultant based in the UK specialising in .Net architecture, design and coding.

He has been working in IT for around 15 years and has worked primarily in the financial sector and is always keen to lead, learn, follow or teach

Comments and Discussions

 
GeneralWcfDeserializeFromString fails when the string was encrypted using AesCryptoServiceProvider Pin
Rabia Kl.30-Dec-08 0:42
Rabia Kl.30-Dec-08 0:42 
I get the following error:
Additional information: There was an error deserializing the object of type XXX '.', hexadecimal value
0x00, is an invalid character. Line 6, position 11.

The same object is moved when I Deserialize without the Encrytion.

Thanks.
Question[Message Deleted] Pin
saudaminib29-Jul-08 10:51
saudaminib29-Jul-08 10:51 
AnswerRe: WCF and XML Serialization Helper Classes Pin
MarkGwilliam30-Jul-08 8:46
MarkGwilliam30-Jul-08 8:46 
GeneralRe: WCF and XML Serialization Helper Classes Pin
saudaminib5-Aug-08 7:58
saudaminib5-Aug-08 7:58 
GeneralRe: WCF and XML Serialization Helper Classes Pin
MarkGwilliam8-Aug-08 9:51
MarkGwilliam8-Aug-08 9:51 
GeneralRe: WCF and XML Serialization Helper Classes Pin
saudaminib26-Aug-08 2:33
saudaminib26-Aug-08 2:33 
GeneralHere is another helpful function - Great Article [modified] Pin
Gary Vidal18-Jan-08 10:49
Gary Vidal18-Jan-08 10:49 
GeneralRe: Here is another helpful function - Great Article Pin
MarkGwilliam19-Jan-08 21:16
MarkGwilliam19-Jan-08 21:16 

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.