---
title: "oneyaml C# cookbook"
url: https://recursiveai.org/oneyaml/csharp/cookbook/
description: "Short recipes for common YAML tasks in C# with OneYaml, each a complete program that was compiled and run against OneYaml 1.2.1."
version: "1.2.1"
---

# oneyaml C# cookbook

Short recipes for common YAML tasks in C# with OneYaml, each a complete program that was compiled and run against OneYaml 1.2.1.

## Recipes

- [Parse YAML into C# classes](https://recursiveai.org/oneyaml/csharp/cookbook/parse-yaml-into-classes/index.md): Read a YAML file into C# classes, records and enums with OneYaml’s YamlSerializer, with naming policies, defaults, required keys and an error for unknown keys.
- [Read YAML without a class](https://recursiveai.org/oneyaml/csharp/cookbook/read-yaml-without-a-class/index.md): Read values from YAML by JSON Pointer in C#, walk mappings and sequences with OneYaml’s YamlNode like a JsonNode, and get each value’s line and column.
- [Edit YAML and keep its comments](https://recursiveai.org/oneyaml/csharp/cookbook/edit-yaml-keep-comments/index.md): Change a value, remove a list item and add keys and list items in a YAML file from C# with OneYaml’s YamlDocument, with every other byte and comment kept.
- [Merge or patch YAML](https://recursiveai.org/oneyaml/csharp/cookbook/patch-yaml/index.md): Apply a Merge Patch (RFC 7396) as an environment overlay and a guarded JSON Patch (RFC 6902) built with YamlPatch to a YAML file in C#, keeping its comments.
- [Convert YAML to JSON and back](https://recursiveai.org/oneyaml/csharp/cookbook/yaml-to-json/index.md): Convert YAML to JSON in C# by reading it into a System.Text.Json JsonNode with OneYaml, and convert JSON back to YAML with YamlSerializer.
- [Read Kubernetes manifests and multi-document YAML](https://recursiveai.org/oneyaml/csharp/cookbook/kubernetes-multi-document/index.md): Read every document of a multi-document YAML file such as a Kubernetes manifest into C# records with DeserializeAll, then edit one document and keep the rest byte for byte.
- [Report errors with line and column](https://recursiveai.org/oneyaml/csharp/cookbook/errors-with-line-and-column/index.md): Catch OneYaml’s YamlException in C# and report the line, column, category and path of a syntax error, a duplicate key or a value that doesn’t fit its type.
- [Anchors, aliases and merge keys](https://recursiveai.org/oneyaml/csharp/cookbook/anchors-aliases-merge-keys/index.md): Read YAML anchors and aliases in C#, turn on << merge keys for docker-compose-style files with MergeKeys, and follow an alias to its anchor in a YamlDocument.
- [Read untrusted YAML safely](https://recursiveai.org/oneyaml/csharp/cookbook/untrusted-input/index.md): Read YAML from the network in C# with YamlLimits.Untrusted, refuse billion-laughs alias bombs and deep nesting, and size the limits for your input.
- [Use YAML in a native AOT app](https://recursiveai.org/oneyaml/csharp/cookbook/native-aot/index.md): Read and write YAML in a trimmed or native AOT .NET app with OneYaml’s source generator, using a YamlSerializerContext instead of reflection.
- [Switch from YamlDotNet with one package](https://recursiveai.org/oneyaml/csharp/cookbook/switch-from-yamldotnet/index.md): Move C# code from YamlDotNet to OneYaml with the OneYaml.Compat.YamlDotNet package, keeping DeserializerBuilder, SerializerBuilder and naming conventions.
