-
-
Notifications
You must be signed in to change notification settings - Fork 78
GraphML Serialization
GraphML is a comprehensive and easy-to-use file format for graphs. QuikGraph supports loading and saving directed graphs to this format. The GraphML serialization APIs are exposed through the GraphMLExtensions extension methods (in QuikGraph.Serialization).
The serialization code uses dynamic code generation to provide high performance in reading and writing custom properties.
To serialize a graph, simply calls the SerializeToGraphML extension methods with an XmlWriter instance.
var graph = new AdjacencyGraph<int, Edge<int>>();
...
using (var xmlWriter = XmlWriter.Create(...))
{
graph.SerializeToGraphML(xmlWriter);
}To deserialize a graph, creates the graph instance and calls the DeserializeFromGraphML.
var graph = new AdjacencyGraph<int, Edge<int>>();
using (var xmlReader = XmlReader.Create(...))
{
graph.DeserializeFromGraphML(
xmlReader,
id => int.Parse(id),
(source, target, id) => new Edge<int>(source, target));
}The serializer supports the XmlAttributeAttribute attribute on public properties of the vertex, edge and graph types. These properties will be declared and serialized in the GraphML stream. The supported property types are bool, int, long, float, double and string.
class IntEdge : Edge<int>
{
...
[XmlAttribute("name")]
public string Name { get; set; }
}Default values can also be specified for the properties by using the DefaultValueAttribute attribute.
...
[XmlAttribute("name")]
[DefaultValue("unknown")]
public string Name { get; set; }