Recursive AI

Edit a YAML multiline string in C# and keep its block style

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

YamlDocument.Set keeps the | or > block style if it can hold the new string.

Terminal
dotnet add package OneYaml

The library targets .NET 8 and later; the example project uses .NET 10.

C#
using OneYaml;
 
using var doc = YamlDocument.Parse("""
    message: |-
      Hello
      world
    active: true
    """);
doc.Set("/message", "Ready\nGo\n");
Console.WriteLine(doc);

If the string cannot be represented in the original style, OneYaml changes the style so the new value can still be read back.

To edit a file instead of an inline string, see how to load and save YAML in C#.

Paste the code above into Program.cs of a new dotnet new console project and dotnet run, or unzip the example and dotnet run.

The snippet prints a literal block with | instead of |-, because the new value ends with one newline:

YAML
message: |
  Ready
  Go
active: true

Variations

Keep a folded block. In the example, use these lines in place of the parse and edit calls. Keep Console.WriteLine(doc); to see the result.

C#
using var doc = YamlDocument.Parse("""
    message: >
      Hello
      world
    active: true
    """);
doc.Set("/message", "Ready to go");
YAML
message: >-
  Ready to go
active: true

The short answer

Parse the YAML with YamlDocument.Parse, then set the key’s value with doc.Set("/message", "Ready\nGo\n"). Pass the string your program should read back, not a piece of YAML with its own | header and indentation. The existing literal block stays literal. OneYaml changes the suffix from - to no suffix so the updated value keeps its final newline. The neighboring active key is not rewritten.

What is the difference between | and > in YAML multiline strings?

The | marker is literal: the break between Ready and Go stays a newline in the value. The > marker is folded: YAML usually reads breaks between ordinary lines of text as spaces; blank lines and more-indented lines have their own rules. Choose a literal block for text where line breaks matter, such as a script, and a folded block for prose you want to wrap in the file without inserting a break at every wrap in the string. OneYaml keeps the existing style where the new value allows it; see the YAML 1.2.2 rules for how each style reads.

What do - and + mean after a YAML block marker?

Those suffixes control chomping, which is what YAML does with line breaks at the end of the block. They don’t change how YAML reads the breaks in the middle. For a nonempty block, |- or >- strips the final break and trailing blank lines. | or > keeps the final break but drops trailing blank lines. |+ or >+ keeps both. See the chomping rules.

In the recipe, the original |- has no newline at the end of the decoded string. The last \n in the C# replacement asks for one, so the header becomes |. In the variation the new string has no final \n, so the folded header becomes >-. In both cases the suffix changes to fit the latest value; preserving block style does not mean keeping the whole header verbatim.

All oneyaml C# recipes