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.