Graphviz4Net provides API to parse the DOT language into a C# data structures. The parsing operation is defined in the IDotParser interface:


public interface IDotParser {
   DotGraph Parse(StreamReader reader);

This interface is implemented by the AntlrParserAdapter class, which uses parser generated by Antlr.

The parsed graph is represented as an instance of DotGraph class, which implements generic interface IGraph. The edges and sub-graphs are represented by instances of the DotEdge and DotSubGraph classes. All these classes provide properties that represent the DOT attributes.

DotGraph is generic class and its nodes can have ids of type int or with ids of type string*. If you are sure that all the nodes in your graph have integral id, you probably want to use the first one. However, if you cannot make any assumptions about ids in your graph, use the second one.

Here is an example of how to use the API (assuming that you have the latest version from VCS):

var parser = AntlrParserAdapter<string>.GetParser();
var result = parser.Parse(
                @"digraph {
                        graph [bb=""0,0,74,112""];
                        Hello [pos=""37,93"", etc....
foreach (var edge in result.VerticesEdges) {
  Console.WriteLine("{0}->{1}", edge.Source.Id, edge.Destination.Id);



* strongly typed int ids are a) used internally by Graphviz4Net when doing layouting, b) more convenient when you are sure that all of your nodes have integral id.

Last edited Aug 8, 2012 at 9:43 AM by stevesindelar, version 3


No comments yet.