Using OzCode's Custom Expression to show object's xml serialization

I’ve been working with OzCode for some time now, and every now and then I find a cool capability I didn’t know it had.
Today I wanted to investigate (read: debug) the way several objects were xml-serialized for a demo I’m preparing and for that I created the following method:
static class Xml
    public static string SerializeObject<T>(this T toSerialize)
        XmlSerializer xmlSerializer = new XmlSerializer(toSerialize.GetType());

        using (StringWriter textWriter = new StringWriter())
            xmlSerializer.Serialize(textWriter, toSerialize);
            return textWriter.ToString();
Now I could use the immediate window to serialize a few classes by calling it:

As you might have guessed I needed to do quite a lot of scrolling and copy-pasting in order to understand this long line of xml.
Since I also needed the value for debugging I decided to try and use Custom Expressions – and it worked!
By using OzCode’s “magic wand” to create a new Custom Expression that called the new method, I got a “new” property in my object that has the full XML serialization result.
Keep in mind that it won’t work when debugging code that cannot access the method used and I recommend using the fully qualified method name (with namespace). And remember – there’s serialization code running underneath the new “property” – so consider deleting the expression once you finish using it (it can always be added back).

With this new property I was able to finish debugging several objects without stopping mid-debug to add code that had no business being there.

Happy coding (&debugging) …

Labels: , ,