This post has been superseded by the following post: Conchango Xml Visualizer for Visual Studio 2005 (RTM)
Updated 06/11/2006: If you are trying to locate the Netron Project see my post "What happened to the Netron Project?"
About a month ago, I stumbled across Scott Nonnenberg's article “Debugger Visualizers on VS 2005 Community Tech Preview”, I was quite intrigued by this article and thought that Debugger Visualizers were a great addition to VS 2005 and an exciting extensibility point. On a previous project, we used the VS 2003 DataSet Visualizer extensively and I had been thinking about trying to create something akin for viewing business entities – but in VS 2003 I just couldn’t get access to the data I wanted.
In mid-March I mentioned the idea of using the Netron Library to visualize XML messages and how I’d wanted to create a VS 2005 Debugger Visualizer to Francois – his reply was that he had already finished work on a Tree-Layout diagram control called Lithium and would I like to “beta” it for him.
How it works:
The solution contains two projects – Conchango.XmlVisualizer (this is the VS 2005 visualizer) and Conchango.XmlVisualizer.Demo (this is the test harness). Conchango.XmlVisualizer contains all the interesting code;
Visualizer is invoked by the IDE when the user selects “Lithium Xml Visualizer” from the dynamic debugger drop down – it creates a modal window and launches MainForm (which contains the Lithium Control) passing in IVisualizerObjectProvider.
On the Debugee side, inside MainForm, IVisualizerObjectProvider.UpdateObject(Stream fromVisualizer, Stream toVisualizer) is called which invokes XmlVisualizerSource.TransferData(object o, Stream fromVisualizer, Stream toVisualizer). o is the object you selected in the IDE to visualize. Inside TransferData o is tested to see if it is an XmlDocument – if it is, the OuterXml is extracted and binary serlialized into a stream and pumped back out of the method using the toVisualizer stream.
The stream is then deserialized back into an XmlDocument (using the LoadXml() method) on the Debugger side and then the node structure is parsed and converted in shape nodes inside the Lithium Control.
Having to extract the XML from XmlDocument.OuterXml seems idiosyncratic – until you get your head around the fact that the XmlDocument type cannot be serialized (it’s not just Xml – there is a lot of state data held inside the object), so we have to extract the raw Xml and then work with that instead.
The AssemblyInfo.cs file contains the DebuggerVisualizer attribute that ties this all together:
[assembly: DebuggerVisualizer(typeof(Conchango.XMLVisualizer.XmlVisualizerSource), typeof(Conchango.XMLVisualizer.Visualizer), 0, Target = typeof(System.Xml.XmlDocument), Description = "Lithium XML Visualizer")]DebuggerVisualizer(typeof(Conchango.XMLVisualizer.XmlVisualizerSource), typeof(Conchango.XMLVisualizer.Visualizer), 0, Target = typeof(System.Xml.XmlDocument), Description = "Lithium XML Visualizer")]
This defines what type the VisualizerSource and Visualizer are, what data type should the visualizer be associated with and what the visualizer is called.
The Conchango.XmlVisualizer project uses a post-build event to copy the build output (Conchango.XmlVisualizer.dll and Lithium.dll) to the Visual Studio Visualizer directory - %USERPROFILE%\My Documents\Visual Studio\Visualizers\
Testing the Visualizer:
- Check that a breakpoint has been set on Conchango.XmlVisualizer.Demo.Program.cs line 56
- Run the solution – Conchango.XmlVisualizer.Demo should load and hit the breakpoint above
- Hover your mouse over the doc object - a dynamic DataTip should appear – click the spyglass icon – a dropdown should appear with an entry called “Lithium Xmll Visualizer”. Select this.
Now the Lithium XmlVisualizer should launch:
There is one warning attached to this visualizer – trying to visualize large Xml structures will turn VS 2005 to treacle and may hang that instance.
The sample XML file in the demo project is a small extract from The Public Whip – a UK site that has lots of interesting raw data about MPs, constituencies and other Parliamentary information – a much more interesting test data source than Northwinds…
I've created a new ProjectDistributor workspace for the project. You can download the source here.