Click here to Skip to main content
Click here to Skip to main content

WCF and XML Serialization Helper Classes

By , 11 Dec 2007
 

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):

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);

// compare instances etc...

// (see unit test project for example)

... 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);

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

  • 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

License

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

About the Author

MarkGwilliam
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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Layout  Per page   
GeneralWcfDeserializeFromString fails when the string was encrypted using AesCryptoServiceProvidermemberRabia Kl.30-Dec-08 0:42 
Question[Message Deleted]membersaudaminib29-Jul-08 10:51 
AnswerRe: WCF and XML Serialization Helper ClassesmemberMarkGwilliam30-Jul-08 8:46 
GeneralRe: WCF and XML Serialization Helper Classesmembersaudaminib5-Aug-08 7:58 
GeneralRe: WCF and XML Serialization Helper ClassesmemberMarkGwilliam8-Aug-08 9:51 
GeneralRe: WCF and XML Serialization Helper Classesmembersaudaminib26-Aug-08 2:33 
GeneralHere is another helpful function - Great Article [modified]memberGary Vidal18-Jan-08 10:49 
GeneralRe: Here is another helpful function - Great ArticlememberMarkGwilliam19-Jan-08 21:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130617.1 | Last Updated 11 Dec 2007
Article Copyright 2007 by MarkGwilliam
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid