forked from YaccConstructor/QuickGraph
-
-
Notifications
You must be signed in to change notification settings - Fork 78
Xml Serialization
Alexandre Rabérin edited this page May 12, 2020
·
2 revisions
QuikGraph 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 module.
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 graph = new AdjacencyGraph<MyVertex, MyEdge>();
...
using (var xwriter = XmlWriter.Create(...))
{
graph.SerializeToXml(
xwriter,
v => v.ID, // Let's use ID as the vertex ID
AlgorithmExtensions.GetEdgeIdentity(graph), // Let QuikGraph give an id to edges
"graph", "myvertex", "myedge", "" // names of the graph, vertex, edge, node xml tags and the namespace uri
);
}To deserialize a graph, create the graph instance and call the DeserializeFromXml.
The serializer supports the delegates to write additional information of the vertex, edge and graph types.