budul.GraphMLReaderWriter
2.0.0
dotnet add package budul.GraphMLReaderWriter --version 2.0.0
NuGet\Install-Package budul.GraphMLReaderWriter -Version 2.0.0
<PackageReference Include="budul.GraphMLReaderWriter" Version="2.0.0" />
<PackageVersion Include="budul.GraphMLReaderWriter" Version="2.0.0" />
<PackageReference Include="budul.GraphMLReaderWriter" />
paket add budul.GraphMLReaderWriter --version 2.0.0
#r "nuget: budul.GraphMLReaderWriter, 2.0.0"
#:package budul.GraphMLReaderWriter@2.0.0
#addin nuget:?package=budul.GraphMLReaderWriter&version=2.0.0
#tool nuget:?package=budul.GraphMLReaderWriter&version=2.0.0
GraphMLReaderWriter
Class library for
- serialization of objects into GraphML files and
- deserialization of GraphML files back into objects.
The graph model is defined by your own classes, marked with attributes. The library also reads node labels and group nodes from files created with yEd.
Installation
dotnet add package budul.GraphMLReaderWriter
Target framework: net10.0.
Usage
Writing
To write a GraphML file there must be a graph containing nodes and edges. The nodes are linked by the edges.
using GraphMLReaderWriter.Attributes;
public class Node
{
[Id]
[Data]
public required string Name { get; set; }
}
public class Edge
{
[Source]
public required Node From { get; set; }
[Target]
public required Node To { get; set; }
}
public class Graph
{
[Edges]
public Edge[] Edges { get; set; } = [];
[Nodes]
public Node[] Nodes { get; set; } = [];
}
Nodes must have a string property marked with the Id attribute. The Data attribute of nodes marks all properties which are used to cover data content in the resulting GraphML data.
Edges must have one node property marked with a Source attribute, this node represents the start of the edge. And edges must have one node property marked with a Target attribute, this node represents the end of the edge.
Nodes and edges must be combined in a joint class. The collection of nodes in this class must be marked with a Nodes attribute. The collection of edges in this class must be marked with an Edges attribute. The collections can be arrays, List<T> or one of the interfaces IList<T>, ICollection<T>, IEnumerable<T> and IReadOnlyList<T>. Nodes can contain nodes of their own, if the node class has a collection marked with a Nodes attribute.
Properties of the joint class marked with the Data attribute are written as data of the graph itself.
Values are written culture independent (decimal point, true and false), and ids must be unique within the graph. The graph can be serialized by using the Writer. A Writer or Reader instance must not be used by several threads at the same time, use one instance per thread.
using GraphMLReaderWriter;
var nodeA = new Node { Name = "a" };
var nodeB = new Node { Name = "b" };
var nodeC = new Node { Name = "c" };
var edgeAB = new Edge { From = nodeA, To = nodeB };
var edgeAC = new Edge { From = nodeA, To = nodeC };
var graph = new Graph
{
Edges = [edgeAB, edgeAC],
Nodes = [nodeA, nodeB, nodeC],
};
var writer = new Writer<Graph>();
writer.Save(
input: graph,
path: path);
The example above creates the following GraphML file.
<?xml version="1.0" encoding="utf-8"?>
<graphml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://graphml.graphdrawing.org/xmlns">
<key id="Key-0" for="node" attr.name="Name" attr.type="string" />
<graph id="Graph-0" edgedefault="directed">
<node id="a">
<data key="Key-0">a</data>
</node>
<node id="b">
<data key="Key-0">b</data>
</node>
<node id="c">
<data key="Key-0">c</data>
</node>
<edge id="Edge-1" source="a" target="b" />
<edge id="Edge-2" source="a" target="c" />
</graph>
</graphml>
Reading
An existing GraphML file can be deserialized by using the Reader.
using GraphMLReaderWriter;
var reader = new Reader<Graph>();
var graph = reader.Load(
path: path);
Migration
1.x to 2.0
- The package targets
net10.0only. Projects on older frameworks stay on 1.x. - The dependency on
UniversalTypeConverteris removed, values are converted by the .NET base class library using the invariant culture. - Values are written culture independent. Numbers use the decimal point, booleans are written as
trueandfalse. - Properties of the graph class marked with
Dataare now written and read. Node data no longer creates akeyfor the graph. - Nested graphs of nodes get the id
<node id>:. - Duplicate ids throw an
InvalidOperationExceptionwhen writing. An edge referencing an unknown node throws anInvalidDataExceptionwhen reading, instead of being skipped silently. - The reader detects the encoding from the file (byte order mark or XML declaration) unless an encoding is passed explicitly. DTD processing is prohibited.
Reader<T>andWriter<T>moved to the namespaceGraphMLReaderWriter. Replace the formerusing GraphMLReader;andusing GraphMLWriter;accordingly.
Build and test
Requires Visual Studio 2026 and the .NET 10 SDK (see global.json).
dotnet build
dotnet test
The tests use xUnit v3 on Microsoft.Testing.Platform. Package versions are managed centrally in Directory.Packages.props.
License
MIT, see LICENSE.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.