Working with YAML in C# (.NET): YamlDotNet Library
YAML (YAML Ain't Markup Language) is a convenient data serialization format often used for configuration files, CI/CD pipeline descriptions, Docker Compose and Kubernetes manifests. In the .NET ecosystem, the primary library for working with YAML is YamlDotNet — a powerful, high-performance tool fully compatible with the YAML 1.1 and 1.2 specifications.
In this article, we will cover: how to install YamlDotNet, perform basic serialization and deserialization of C# objects, configure custom tags, and handle complex data structures.
Installing YamlDotNet
The library is available via NuGet. Run the command in the package manager console:
dotnet add package YamlDotNetOr via Package Manager Console:
Install-Package YamlDotNetBasic Usage: Deserializing YAML into a C# Object
Suppose we have a YAML file config.yaml with application settings:
app: name: MyApp version: 2.0.1 debug: truedatabase: host: localhost port: 5432 credentials: user: admin password: secretLet's create the corresponding C# classes:
public class AppConfig{ public string Name { get; set; } public string Version { get; set; } public bool Debug { get; set; }}
public class DatabaseConfig{ public string Host { get; set; } public int Port { get; set; } public CredentialsConfig Credentials { get; set; }}
public class CredentialsConfig{ public string User { get; set; } public string Password { get; set; }}
public class RootConfig{ public AppConfig App { get; set; } public DatabaseConfig Database { get; set; }}Now let's deserialize the YAML into an object:
using YamlDotNet.Serialization;using YamlDotNet.Serialization.NamingConventions;
var yamlContent = File.ReadAllText("config.yaml");
var deserializer = new DeserializerBuilder() .WithNamingConvention(UnderscoredNamingConvention.Instance) // for snake_case .Build();
var config = deserializer.Deserialize<RootConfig>(yamlContent);
Console.WriteLine(config.App.Name); // MyAppConsole.WriteLine(config.Database.Port); // 5432Serializing C# Objects to YAML
The reverse task — converting a C# object into a YAML string:
var config = new RootConfig{ App = new AppConfig { Name = "MyApp", Version = "2.0.1", Debug = true }, Database = new DatabaseConfig { Host = "localhost", Port = 5432, Credentials = new CredentialsConfig { User = "admin", Password = "secret" } }};
var serializer = new SerializerBuilder() .WithNamingConvention(CamelCaseNamingConvention.Instance) // camelCase in YAML .Build();
var yaml = serializer.Serialize(config);Console.WriteLine(yaml);Result:
app: name: MyApp version: 2.0.1 debug: truedatabase: host: localhost port: 5432 credentials: user: admin password: secretWorking with Collections and Complex Types
YamlDotNet fully supports lists, dictionaries, and nested structures. Example with a list:
public class Project{ public string Name { get; set; } public List<string> Tags { get; set; }}
// YAML:// name: MyProject// tags:// - dotnet// - yaml// - csharp
var yaml = @"name: MyProjecttags: - dotnet - yaml - csharp";
var deserializer = new DeserializerBuilder().Build();var project = deserializer.Deserialize<Project>(yaml);
foreach (var tag in project.Tags) Console.WriteLine(tag);Custom Tags and Types (TagMapping)
Sometimes YAML files contain explicit t
Also in library
Sending Email in Swift: Libraries and Examples for iOS/macOS
Caching in Node.js: Redis and Alternatives for Accelerating Applications
Data Validation in Java: Overview of Libraries and Approaches