---
title: "Switch from YamlDotNet with one package"
url: https://recursiveai.org/oneyaml/csharp/cookbook/switch-from-yamldotnet/
description: "Move C# code from YamlDotNet to OneYaml with the OneYaml.Compat.YamlDotNet package, keeping DeserializerBuilder, SerializerBuilder and naming conventions."
version: "1.2.1"
---

# Switch from YamlDotNet with one package

`OneYaml.Compat.YamlDotNet` is a subset of YamlDotNet’s API on top of OneYaml: the serializer builders and the representation model, tested against YamlDotNet 18.1.0. Swap the package and the namespaces, and the builder calls stay.

```sh
dotnet remove package YamlDotNet
dotnet add package OneYaml.Compat.YamlDotNet --version 1.2.1
```

```csharp
using OneYaml.Compat.YamlDotNet.Serialization;                   // was: YamlDotNet.Serialization
using OneYaml.Compat.YamlDotNet.Serialization.NamingConventions; // was: YamlDotNet.Serialization.NamingConventions

// The same builders and calls as YamlDotNet.
IDeserializer deserializer = new DeserializerBuilder()
    .WithNamingConvention(CamelCaseNamingConvention.Instance)
    .IgnoreUnmatchedProperties()
    .Build();
var service = deserializer.Deserialize<Service>("name: web\nreplicas: 2\nlegacy: true\n");
Console.WriteLine($"{service.Name} x{service.Replicas}");

ISerializer serializer = new SerializerBuilder()
    .WithNamingConvention(CamelCaseNamingConvention.Instance)
    .Build();
Console.Write(serializer.Serialize(service));

class Service
{
    public string Name { get; set; } = "";
    public int Replicas { get; set; }
}
```

```text
web x2
name: web
replicas: 2
```

## Good to know

- **Not covered:** static contexts, type inspectors, node deserializers, the scanner and comment events. The [package page](https://recursiveai.org/oneyaml/csharp/docs/compat-yamldotnet/) lists what is covered and every difference.
- The compatibility package is a bridge. For new code, `YamlSerializer` and `YamlDocument` in the `OneYaml` package read YAML 1.2, keep comments when you edit, and support native AOT.
- From SharpYaml, the bridge is `OneYaml.Compat.SharpYaml` ([package page](https://recursiveai.org/oneyaml/csharp/docs/compat-sharpyaml/), [migration guide](https://recursiveai.org/oneyaml/csharp/docs/migrate-sharpyaml/)).

What to check after the swap, and how to move to the `OneYaml` API itself, are in [Migrating from YamlDotNet](https://recursiveai.org/oneyaml/csharp/docs/migrate-yamldotnet/).

A longer guide: [Replacing YamlDotNet](https://recursiveai.org/oneyaml/articles/yamldotnet-alternative/).
