Recursive AI

Build a YAML patch programmatically in C#

OneYaml is a free, open-source YAML library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

Use YamlPatch to add or replace keys with C# values, without writing JSON.

Terminal
dotnet add package OneYaml

Targets .NET 8 and .NET 10 (example).

C#
using OneYaml;
 
using var doc =
    YamlDocument.Parse("port: 8080\n");
var patch = new YamlPatch()
    .Replace("/port", 9090)
    .Add("/enabled", true);
 
doc.ApplyPatch(patch);
Console.Write(doc);

Unzip the example, then dotnet run.

The snippet prints:

Text
port: 9090
enabled: true

The paths /port and /enabled name keys at the root. If a Replace path doesn’t exist, ApplyPatch throws a YamlException and leaves the document unchanged.

Variations

Remove a key only if its value still matches. With the same doc declaration, replace the patch builder:

C#
var patch = new YamlPatch()
    .Test("/port", 8080)
    .Remove("/port");

This prints {} because port was the only key. If port no longer equals 8080, the test fails and the removal doesn’t run.

The short answer

Create a YamlPatch, add operations such as Replace and Add with C# values, and call doc.ApplyPatch(patch). You don’t need to produce JSON text first. In the example, 9090 is an integer and true is a boolean; the builder gets them directly from C# rather than interpreting strings that look like numbers or booleans.

How do I update a YAML file programmatically in C#?

Use YamlDocument to edit existing text when you want untouched values, layout and comments to remain. Each operation takes a JSON Pointer. /port is a root key; /server/port goes through the server mapping first. If you want to change a key that must already exist, use Replace. Use Add for a new mapping key or to set a key whether or not it already exists. It doesn’t create missing parent mappings.

The builder converts values when you add the operations. Once built, the same patch can be applied to multiple documents that have the expected paths. It takes plain values including booleans, numbers and strings, as well as dictionaries and sequences of those values. The patch guide covers the overloads for custom types. ApplyPatch updates the document in memory. To patch a file, put port: 8080 in settings.yaml, replace the snippet’s doc declaration with the first line below, and add the second line after doc.ApplyPatch(patch):

C#
using var doc = YamlDocument.Parse(File.ReadAllText("settings.yaml"));
File.WriteAllText("patched.yaml", doc.ToString());

This writes the YAML shown above to patched.yaml and leaves settings.yaml unchanged.

Can I test a YAML value before patching it?

Yes. Put Test before the change if the edit depends on a starting value. Operations run in the order you added them. If any fails, OneYaml rolls back the whole call, including successful operations earlier in the same patch. The YamlException.OperationIndex identifies the failed operation, starting at zero. That is an offset into the patch, not a YAML line number.

Watch the scalar type when you test a value. OneYaml compares YAML values: a test against integer 8080 doesn’t match a string "8080" or a float 8080.0. See value comparison in patches before reusing a patch originally made for JSON.

All oneyaml C# recipes