A generic XML string serialiser

The following class is a simple XML string serialiser which will work with most .NET objects except WCF data contracts which require the NetDataContractSerializer. The static methods Serialise and Deserialise convert an object instance to an XML string and vice versa.

Here is an example model I used for testing. Notice it implements IComparable<> so I can compare expected and actual values in my test methods.

And here is my unit-test using the Visual Studio test framework, to check the class works correctly. Notice the use of a mock object and embedded XML resource file (not shown) which provide sample data to test against:

And here are the results of the test run:

Unit-testing is the cornerstone of test-driven development (TDD) and is an essential requirement whenever you are planning to extend or refactor existing code which has passed integration/UAT testing, especially if it is already in use in a production environment. Unit-tests can identify when changes unintentionally break existing functionality, which is always the number one risk of modifying working code.

Advertisements

About Phil Munro

I have been developing commercial desktop and distributed web applications with Microsoft technologies since 1997.
This entry was posted in C#, NUnit, TDD. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s