views:

328

answers:

2

I've started messing around with the DGML viewer in VS 2010 (which seems awesome). I know you can create diagrams from your code base.

Is there any support for creating a directed graph from whatever I like, or is it purely a code analysis tool? I'd like something along the lines of 'Add Node' or something.

+1  A: 

You would have to generate the graph manually. As far as i know there are no visual tools to modify its structure on this level.

If you rightclick in the white space you can select View DGML which will show you the source XML file and modify it. When you know the structure, you can as well generate the graph automatically from your code simply by creating a XML file with proper structure. All you need to define is Links section. You can as well define Nodes section if you wish to have some nice looking names in the graph.

Here goes an example how you can get it done. The are most likely more efficient ways to do it, but this one is easy to understand.

        XmlWriter xmlWriter = XmlWriter.Create(outputFile, new XmlWriterSettings() { Encoding = Encoding.UTF8 });
        xmlWriter.WriteStartDocument();
        xmlWriter.WriteStartElement("DirectedGraph", "http://schemas.microsoft.com/vs/2009/dgml");
        xmlWriter.WriteStartElement("Nodes");
        // dump nodes
        foreach (Name n in Names)
        {
            xmlWriter.WriteStartElement("Node");
            xmlWriter.WriteAttributeString("Id", n.Id); // id is an unique identifier of the node 
            xmlWriter.WriteAttributeString("Label", n.Label); // label is the text on the node you see in the graph
            xmlWriter.WriteEndElement();
        }
        xmlWriter.WriteEndElement();
        xmlWriter.WriteStartElement("Links");
        // dump links
        foreach (Link l in Links)
        {
            xmlWriter.WriteStartElement("Link");
            xmlWriter.WriteAttributeString("Source", l.Source); // ID! of the source node
            xmlWriter.WriteAttributeString("Target", l.Target); // ID of the target node 
            xmlWriter.WriteEndElement();
        }
        xmlWriter.WriteEndElement();
        xmlWriter.WriteEndElement();
        xmlWriter.WriteEndDocument();
        xmlWriter.Close();

this is all you need to do .. if you dont want any grouping or coloring .. that would add a bit more code ...

ilandra
A: 

You can find basic editing DGML tasks here: How to Edit and Customize Graph Documents

Esther Fan - MSFT