Skip to content
  • English Deutsch

benjamin-abt.com

Performance is a feature | Cloud - AI - Azure - .NET - Sustainability

  • Home
  • Blog
  • Portfolio
    • Overview
    • Workshops & Training
    • Publications
  • Contact
  • Home
  • Blog

Handle Yaml Files with .NET

Handle Yaml Files with .NET
April 23, 2024 5 min read BEN ABT
Csharp .NET

YAML files are unfortunately part of everyday life for all developers these days; and although they are very error-prone and almost impossible to edit without an IDE and schema information without constantly running into errors - many greetings to all CI systems that think this was a good idea: it wasn’t - we have to accept that we have to process them.

In .NET, the YamlDotNet library is a popular choice for processing YAML files. This library is very easy to use and provides very good support for most YAML files.

When processing YAML files, YamlDotNet offers two different options: key-value access via a dictionary or deserialization into a class. Both methods have their advantages and disadvantages, but in most cases deserialization into a class is the more convenient and better choice.

Deserialize from YAML

For a deserialization into a class, corresponding classes are also necessary; as an example I take a structure of a potential header of a blog post:

YAML
 1title: Handle Yaml Files with .NET
 2description: This blog post shows a simple sample how to serialize and deserialize yaml files with .NET
 3
 4options:
 5    isDraft: true
 6    date: 2024-04-23T15:30:00Z
 7author:
 8    name: BEN ABT
 9    twitter: https://twitter.com/Abt_Benjamin
10    linkedIn: https://www.linkedin.com/in/benjaminabt/
11    job:
12        company: Medialesson GmbH
13        description: Chief PullRequest Officer
14        website: https://media-lesson.com/

Accordingly, the class structure can be implemented 1:1:

C#
 1public class BlogPost
 2{
 3    public string Title { get; set; } = null!;
 4    public string Description { get; set; } = null!;
 5    public Options Options { get; set; } = null!;
 6    public Author Author { get; set; } = null!;
 7    public string Content { get; set; } = null!;
 8}
 9public class Options
10{
11    public bool IsDraft { get; set; }
12    public DateTimeOffset Date { get; set; }
13}
14public class Author
15{
16    public string Name { get; set; } = null!;
17    public string Twitter { get; set; } = null!;
18    public string LinkedIn { get; set; } = null!;
19    public Job Job { get; set; } = null!;
20}
21public class Job
22{
23    public string Company { get; set; } = null!;
24    public string Description { get; set; } = null!;
25    public string Website { get; set; } = null!;
26}

As of today, no records are supported in YamlDotNet; the deserializer needs a class with an empty constructor and the corresponding setters in the properties.

Deserialization is then very simple:

C#
1IDeserializer deserializer = new DeserializerBuilder()
2    .WithNamingConvention(CamelCaseNamingConvention.Instance)
3    .Build();
4
5// yamlBlogPost = string with yaml content
6BlogPost blogPost = deserializer.Deserialize<BlogPost>(yamlBlogPost); 

The blogPost object then contains the corresponding values from the YAML file as an instance, which can be accessed as usual.

Serialize to YAML

Serialization is just as easy - only in reverse:

C#
1ISerializer serializer = new SerializerBuilder()
2    .WithNamingConvention(CamelCaseNamingConvention.Instance)
3    .Build();
4
5string blogPostYaml = serializer.Serialize(blogPost);

The complete sample

C#
 1// --------------------------------------------------------------------
 2// This sample shows the handling of YAML files in .NET.
 3// The YamlDotNet library (https://github.com/aaubry/YamlDotNet) is used to serialize and deserialize YAML files.
 4// 2024-04-23 - https://schwabencode.com
 5
 6// Runtime: .NET 8
 7// Sample Project: Console App
 8// Dependency: YamlDotNet
 9
10using YamlDotNet.Serialization;
11using YamlDotNet.Serialization.NamingConventions;
12
13// --------------------------------------------------------------------
14// sample Yaml-File in style of a blog post header
15string yamlBlogPost =
16    """
17    title: Handle Yaml Files with .NET
18    description: This blog post shows a simple sample how to serialize and deserialize yaml files with .NET
19
20    options:
21        isDraft: true
22        date: 2024-04-23T15:30:00Z
23    author:
24        name: BEN ABT
25        twitter: https://twitter.com/Abt_Benjamin
26        linkedIn: https://www.linkedin.com/in/benjaminabt/
27        job:
28            company: Medialesson GmbH
29            description: Chief PullRequest Officer
30            website: https://media-lesson.com/
31    """;
32    
33// --------------------------------------------------------------------
34// deserialize string as model
35IDeserializer deserializer = new DeserializerBuilder()
36    .WithNamingConvention(CamelCaseNamingConvention.Instance)
37    .Build();
38
39BlogPost blogPost = deserializer.Deserialize<BlogPost>(yamlBlogPost);
40
41// Print blog post
42Console.WriteLine(new string('-', 30));
43Console.WriteLine($"## Print Blog Post Object");
44Console.WriteLine($"Title: {blogPost.Title}");
45Console.WriteLine($"\tDescription: {blogPost.Description}");
46Console.WriteLine($"Options");
47Console.WriteLine($"\tIs Draft: {blogPost.Options.IsDraft}");
48Console.WriteLine($"\tDate: {blogPost.Options.Date:o}");
49Console.WriteLine($"Author");
50Console.WriteLine($"\tName: {blogPost.Author.Name}");
51Console.WriteLine($"\tTwitter: {blogPost.Author.Twitter}");
52Console.WriteLine($"\tLinkedIn: {blogPost.Author.LinkedIn}");
53Console.WriteLine($"\tJob");
54Console.WriteLine($"\t\tCompany: {blogPost.Author.Job.Company}");
55Console.WriteLine($"\t\tDescription: {blogPost.Author.Job.Description}");
56Console.WriteLine($"\t\tWebsite: {blogPost.Author.Job.Website}");
57
58// --------------------------------------------------------------------
59// create yaml serializer with options
60ISerializer serializer = new SerializerBuilder()
61    .WithNamingConvention(CamelCaseNamingConvention.Instance)
62    .Build();
63
64string blogPostYaml = serializer.Serialize(blogPost);
65
66Console.WriteLine(new string('-', 30));
67Console.WriteLine($"## Print Blog Post Text");
68Console.WriteLine(blogPostYaml);
69
70// --------------------------------------------------------------------
71// sample models as classes, records are not supported today
72public class BlogPost
73{
74    public string Title { get; set; } = null!;
75    public string Description { get; set; } = null!;
76    public Options Options { get; set; } = null!;
77    public Author Author { get; set; } = null!;
78    public string Content { get; set; } = null!;
79}
80public class Options
81{
82    public bool IsDraft { get; set; }
83    public DateTimeOffset Date { get; set; }
84}
85public class Author
86{
87    public string Name { get; set; } = null!;
88    public string Twitter { get; set; } = null!;
89    public string LinkedIn { get; set; } = null!;
90    public Job Job { get; set; } = null!;
91}
92public class Job
93{
94    public string Company { get; set; } = null!;
95    public string Description { get; set; } = null!;
96    public string Website { get; set; } = null!;
97}

PS: please do not use Yaml, if you don’t have to. Everyone hates yaml. Thanks.

Related articles

Containerizing .NET in 2026 Without a Dockerfile

Oct 20, 2026 - 11 min read

Containerizing .NET in 2026 Without a Dockerfile

Most .NET Dockerfiles repeat the same five steps: restore, publish, select the matching Microsoft runtime image, copy the output and start …

Problem Details in ASP.NET Core and .NET 11

Oct 5, 2026 - 12 min read

Problem Details in ASP.NET Core and .NET 11

builder.Services.AddProblemDetails() registers infrastructure. It does not create an error contract. An ASP.NET Core request can fail during …

Resilience in .NET with Microsoft.Extensions.Resilience

Sep 21, 2026 - 11 min read

Resilience in .NET with Microsoft.Extensions.Resilience

Retries are the most visible resilience strategy and the easiest one to misuse. Repeating a failing call can recover from a short network …


BEN ABT

BEN ABT

Ben is Chief Engineering Officer and Principal Software Engineer at Medialesson.de, with a strong focus on .NET and Microsoft Azure. In his professional role, he designs and builds highly scalable platforms for hybrid and cloud-based scenarios. He also advises companies and C-levels on migrating to the sovereign cloud.

He runs myCSharp.de, the largest and most active German-speaking C# community forum, actively contributes to both his own and external open-source projects and regularly shares his knowledge through his blog, at community events, conferences and as a writer for technical magazines.

Let's Work Together

Looking for an experienced Platform Architect or Engineer for your next project? Whether it's cloud migration, platform modernization or building new solutions from scratch - I'm here to help you succeed.

New Platforms

Modernization

Training & Consulting

Contact Me
X Facebook LinkedIn WhatsApp
Back Next Blog Post

Open for work!

Contact Me
BEN ABT BEN ABT Software Engineer · Microsoft MVP Passionate about .NET, Azure, performance optimization and sustainable software engineering.

Medialesson GmbH Logo

Microsoft MVP Logo

Recent posts

View all
Stop Using nginx as Your Default Static File Server in Docker

Stop Using nginx as Your Default Static File Server in …

Oct 24, 2026
Containerizing .NET in 2026 Without a Dockerfile

Containerizing .NET in 2026 Without a Dockerfile

Oct 20, 2026
Problem Details in ASP.NET Core and .NET 11

Problem Details in ASP.NET Core and .NET 11

Oct 5, 2026
Resilience in .NET with Microsoft.Extensions.Resilience

Resilience in .NET with Microsoft.Extensions.Resilience

Sep 21, 2026
What Matters in ASP.NET Core 11

What Matters in ASP.NET Core 11

Sep 14, 2026

Tags

.NET (192) Csharp (96) ASP.NET Core (48) Azure (41) DevOps (17) Docker (14) Microsoft (14) EF Core (11) Microsoft MVP (11) AI (10) GitHub (10) GitHub Copilot (10) ASP.NET (9) Security (9) Synology (9) Architecture (8) Raspberry (8) Azure Functions (7) Cloudflare (7) Bicep (6)
benjamin-abt.com

About BEN

Benjamin Abt is a Software Engineer, Platform Architect and Microsoft MVP specializing in .NET, Azure, cloud architecture, performance optimization and sustainable software development.

Connect

  • Follow me on GitHub
  • Follow me on LinkedIn
  • Follow me on Mastodon
  • Microsoft MVP profile
  • Follow SchwabenCode on GitHub
  • Follow me on X

Quick Links

  • Portfolio
  • Blog
  • Workshops & Training
  • Publications
  • Get in Touch
  • Sitemap
Open for Work

© Copyright Benjamin Abt & SchwabenCode | 2011 - 2026

Content licensed under CC BY 4.0 unless otherwise noted.

  • Impressum
  • Privacy
  • Contact