How to Compare XML Strings Using .NET

So you want to compare two XML strings together to see if they are equal. Obviously, it would be easy to just compare the two strings using a regular string equality comparison and call it a day. However, what if the XML strings looked like the following:

<SampleXML>
    <SampleField1>Testing</SampleField1>
    <SampleField2 value="test value" />
    <SampleField3 type="int">456</SampleField3>
</SampleXML>
<SampleXML>
        <SampleField1>Testing</SampleField1>
    <SampleField2     value="test value" />
        <SampleField3 type="int">456</SampleField3></SampleXML>

All of the nodes, attributes and values are the same, but the formatting is way off. Obviously, a string equality comparison between these two would return false. Is there anything that can be done about this? Not to fear, .NET provides a pretty simple solution. Continue reading

Serialization and Deserialization

Serialization is a process of converting an object into a stream of data, to easily transfer over the network or to save to disk.   So, using this concept of serialization, we can serialize any object to XML string.
Here is a person class that we can serialize.


Here is the code that takes the person object into string of xml format.

Using Serialization in .NET is provided by the System.Runtim.Serialization name space.

Once you have the XML string, now you can save it to disk, store it into database, or transfer over the network.
Here is the deserialization method that takes serialized string of XML into the person object.

Kyoungsu Do
Software Quality Engineer
ImageSource, Inc.