---
title: "Build a YAML patch programmatically in C#"
url: https://recursiveai.org/oneyaml/csharp/cookbook/patch-yaml-from-csharp-operations/
description: "Build a YAML patch with typed C# operations instead of JSON text. Replace an existing key, add a boolean and apply the patch with OneYaml."
version: "1.2.1"
---

# 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.

[NuGet](https://www.nuget.org/packages/OneYaml) · [GitHub](https://github.com/recursive-ai-oss/oneyaml-csharp) · [Docs](https://recursiveai.org/oneyaml/csharp/docs/) · [Download example (.zip)](https://recursiveai.org/oneyaml/csharp/cookbook/patch-yaml-from-csharp-operations/patch-yaml-from-csharp-operations.zip)

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

```shell
dotnet add package OneYaml
```

Targets .NET 8 and [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (example).

```csharp
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](https://recursiveai.org/oneyaml/csharp/cookbook/patch-yaml-from-csharp-operations/patch-yaml-from-csharp-operations.zip), 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:

```csharp
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.

## Related recipes

- [Apply a JSON Patch to a YAML file](https://recursiveai.org/oneyaml/csharp/cookbook/patch-yaml/)
- [Patch the second document in a YAML stream](https://recursiveai.org/oneyaml/csharp/cookbook/rewrite-yaml-documents-in-stream-by-index/)

## 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](https://www.rfc-editor.org/rfc/rfc6901). `/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](https://recursiveai.org/oneyaml/csharp/docs/patches/#building-a-patch-in-code) 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)`:

```csharp
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](https://recursiveai.org/oneyaml/csharp/docs/patches/#differences-from-rfc-6902) before reusing a patch originally made for JSON.
