budul.GraphMLReaderWriter 2.0.0

dotnet add package budul.GraphMLReaderWriter --version 2.0.0
                    
NuGet\Install-Package budul.GraphMLReaderWriter -Version 2.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="budul.GraphMLReaderWriter" Version="2.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="budul.GraphMLReaderWriter" Version="2.0.0" />
                    
Directory.Packages.props
<PackageReference Include="budul.GraphMLReaderWriter" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add budul.GraphMLReaderWriter --version 2.0.0
                    
#r "nuget: budul.GraphMLReaderWriter, 2.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package budul.GraphMLReaderWriter@2.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=budul.GraphMLReaderWriter&version=2.0.0
                    
Install as a Cake Addin
#tool nuget:?package=budul.GraphMLReaderWriter&version=2.0.0
                    
Install as a Cake Tool

GraphMLReaderWriter

NuGet License: MIT

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.0 only. Projects on older frameworks stay on 1.x.
  • The dependency on UniversalTypeConverter is 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 true and false.
  • Properties of the graph class marked with Data are now written and read. Node data no longer creates a key for the graph.
  • Nested graphs of nodes get the id <node id>:.
  • Duplicate ids throw an InvalidOperationException when writing. An edge referencing an unknown node throws an InvalidDataException when 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> and Writer<T> moved to the namespace GraphMLReaderWriter. Replace the former using GraphMLReader; and using 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • 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.

Version Downloads Last Updated
2.0.0 97 11/30/2026
1.1.3 901 2/15/2022
1.1.2 669 1/30/2022
1.1.1 660 1/29/2022
1.1.0 624 1/29/2022
1.0.1 664 1/29/2022
1.0.0 644 1/29/2022