Utilizing the Directed Graph document in Visual Studio

Have you noticed  that there is a Directed Graph document (.dgml) in Visual Studio? This document can visualize directed graphs inside VS. This type of document is authored with the tools of the Ultimate edition. If you do not have the Ultimate edition, the editor allows for manual creation of nodes and links, which may be useful in some situations but cumbersome at best.

The .dgml file follows a simple xml schema and is pretty easy to generate yourself. So I downloaded the .xsd file (you can use the xml namespace as an url and it will present you a page with info on this) and generated some classes with xsd.exe.

Now you have to build a DirectedGraph class instance with all the Nodes and Links that represent the information you are trying to visualize. This will require you to write some custom code (a factory method/class for instance) that transforms the data structure of your program into Nodes and Links.

I have done this for a project I am working on at home. The data structure I wanted to visualize represents a schema (similar to an xml schema) but each ‘node’ has a whole lot of navigation properties. A small schema looks something like this:


Depending on the properties you set for each Node and Link object you can change Font, Colors, Lines, Labels etc.

Be aware that any changes you make to the document are saved to the .dgml file. If you want these changes reflected back to your own structure, you have to write an interpreter for that – not something I would recommend.

Be sure to explore the capabilities of the .dgml editor – there are some nice features there (Analyzers).

I can use this as a debugging tool to see if my schema is converted correctly into this structure. I needed about 150 lines of very simple code to create my factory.

Hope it helps,