Skip to content

GraphML Serialization

Eivind Gussiås Løkseth edited this page Aug 3, 2018 · 6 revisions

GraphML Serialization

GraphML is a comprehensive and easy-to-use file format for graphs. QuickGraph supports loading and saving directed graphs to this format. The GraphML serialization APIs are exposed through the {{GraphMLExtensions}} extension methods (in {{QuickGraph.Serialization}}.

The serialization code uses dynamic code generation to provide high performance in reading and writing custom properties.

Serializing graphs

To serialize a graph, simply call the {{SerializeToGraphML}} extension methods with an {{XmlWriter}} instance. {{ var g = new AdjacencyGraph<int, Edge>(); ... using(var xwriter = XmlWriter.Create(...)) g.SerializeToGraphML(xwriter); }}

Deserializing graphs

To deserialize a graph, create the graph instance and call the {{DeserializeFromGraphML}}: {{ var g = new AdjacencyGraph<int, Edge>(); using(var xreader = XmlReader.Create(...)) g.DeserializeFromGraphML(xreader, id => int.Parse(id), (source, target, id) => new Edge(source, target) ); }}

Serializing Custom Fields

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 { ... 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;} }}

Clone this wiki locally