This project is read-only.

Xml Serialization

QuickGraph supports loading and saving graphs to and from Xml. The implementation builds on top of XmlReader and XmlWriter and is located in the SerializationExtensions class from the QuickGraph.Serialization namespace.
Serializing graphs
To serialize a graph, call the SerializeToXml extension methods with an XmlWriter instance and delegates that can map vertices and edges to an identifier string. The serialization method also supports custom delegate to write additional information per vertex, edge.
class MyVertex {
    public int ID { get;set; } 
class MyEdge : IEdge<MyVertex> {
    string Name { get; set; }
    string Tag { get; set; }
var g = new AdjacencyGraph<MyVexte, MyEdge>();
using(var xwriter = XmlWriter.Create(...))
        v => v.ID, // let's use ID as the vertex ID
        AlgorithmExtensions.GetEdgeIdentity(graph), // let QuickGraph give an id to edges
        "graph", "myvertex", "myedge", "" // names of the graph, vertex, node xml tags and the namespace uri

Deserializing graphs

To deserialize a graph, create the graph instance and call the DeserializeFromXml.
Serializing Custom Fields
The serializer supports the delegates to write additional information of the vertex, edge and graph types.

Last edited Mar 28, 2009 at 9:22 PM by pelikhan, version 1


timothypratley Oct 29, 2013 at 11:29 PM 
[reference and using Microsoft.VisualStudio.GraphModel;]

timothypratley Oct 29, 2013 at 11:28 PM 
This did the trick for me, though it seems like there might be a more elegant way:

public static MyGraph LoadDGML(string filename)
var g = Graph.Load(filename);

//graph where the vertices and edges should be put in
var pocGraph = new MyGraph();
foreach (var n in g.Nodes)
pocGraph.AddVertex(new MyVertex(n.Id.ToString()));
var indexed = pocGraph.Vertices.ToDictionary(v => v.ID);
foreach (var e in g.Links)
pocGraph.AddEdge(new MyEdge(indexed[e.Source.Id.ToString()], indexed[e.Target.Id.ToString()]));
return pocGraph;

timothypratley Oct 29, 2013 at 10:52 PM 
Can you provide an example of xml deserialization? I am trying to deserialize a dgml file, and when constructing the edges, I do not know how to access the nodes.

itsmemario Sep 30, 2010 at 2:53 PM 
here's a thread about (de)serializing custom fields. Hope this'll help someone.

orest86 Dec 22, 2009 at 6:09 PM 
Could you give me some advices on how to write delegates for additional information of the vertex (Serializing custom fields)?