c sharp

How do I Parse a YAML File in C Sharp?

“Development in C# is versatile and general-purpose. The programming paradigms of declarative, procedural, operational, universal, object-oriented (class-based), intrinsic typing, powerful typing, lexically zoomed, and element-oriented are all covered by the C# language. Microsoft’s C# (C-Sharp) programming, which operates on the.NET Platform, is often used to create a wide range of applications, including online, pc, smartphone, and gaming programs. Composing configuration settings often involves using the data serialization technology YAML. YAML can mean either just another scripting language or YAML ain’t markup language (a cyclical acronym), highlighting the fact that YAML is only for data rather than texts. YAML is a well-known scripting language since it is simple to grasp and human-readable. Additionally, it may be combined with some other computer languages.”

Example Code

Let’s get started with the implementation of the C# code example in Visual Studio code to parse the YAML file in it. Firstly, you need to launch the Visual Studio Code and create a new file of C# type in it. Make sure to create it within the current working directory of your Windows system. So, we have been starting our C# example script with the inclusion of some of the very main, basic, and necessary libraries to make it work. Without these libraries, our code may work at all. The very first library, “System” has been including the Collections namespace that has been holding the generic functions and interfaces in it. We need to use the “Generic” keyword along with that. You can also take Collections as a simple collection of some generic C# functions and structures. After this, we have been using the YamlDotNet package of C# to use the YAML file format in this code along with the Serialization namespace, i.e., holding many YAML parsing functions.

Along with that, the NamingConventions namespace or generic class has been specified to use the naming convention function in this code. We have been initializing two public classes named Info and Person. Each of these classes holds different variables. The first class, “Info,” contains three string-type variables named street, city, and country. The get and set functions of C# are used here to get the values for all these variables from the main() function and set these all here. The second public class, “Person,” contains 1 string variable named “Name”, 1 integer variable named “Age”, and 1 float variable named “Height” for a specific person. The getter and setter functions are used once again to set the values for these three variables.

Then, we have been using the Dictionary collection of string types that have been declared public here, implementing the “Info” class. This Dictionary collection has been named “Addresses” to store the address information of a person using the get and set functions of C# in the curly brackets. All these variables are used as access specifies for this YAML code.

Now, we have initialized another public class named “Program” within the same C# code file and initialized a main() function implementation within it. We have been creating a variable object “person” for the class “Person” in this. Within this object, we have initialized the variable “Name”, “age”, and “Height” with some values and created an object for Dictionary “Addresses”. For the Height variable, we have been applying some float formula to calculate the height of the person. Within this dictionary, we have been using the 2 different keys with their values and the “object” of the “Info” class as the second parameter.

Within both these second parameters of the “info” class, we have been passing values to the Street, City, and Country variables of the Info class. According to this, there would be 4 main keys of the YAML file format along with 2 inner key-pair values. The variable “person” has been closed here.

The serializerBuilder() function from the Serializer namespace of C# has been used for the initialization of serializer variables. This function has been calling the “with naming convention” class calling its instance using the “CamelCaseNamingConvention” object to build this serialization via the use of the “Build()” function. The “yaml” variable has been initialized with the use of the “Serialize()” function using the “person” object of the “Person” class using the serializer package.

This means the YAML file format initialized within the “Person” class will be displayed using the System.Console.WriteLine() function statement of C# i.e. utilizing the “yaml” variable here. We have initialized another variable, “yaml,” to create a “YAML” file containing name, age, height, and addresses as their main keys and some internal key-pair values. The DeserializerBuilder() function by the deserializer object has been called with the WithNamingConvention function with the “UnderscoreNamingConvention” class calling its instance, followed by the Build() function to deserialize the YAML file. The variable “p” has been using the “Person” class as “YAML” data to deserialize it via the use of the deserializer package.

The “home” key has been used for the “Addresses” dictionary passed to the deserializer variable. This is deserializing the YAML “yml” file to the normal text using the Console.WriteLine() function of “System” library. The Name, Age, Street, City, and Country have been used from the “yml” YAML file format to be deserialized to the normal line and printed out. The main() function, along with its Program class, has been completed here.

We have saved the code and executed it within the VS Code. The output has been displaying the YAML file for the “Person” object as it is, while the deserialized “yml” YAML file has been displayed within a single line at the end.


This article’s first paragraph elaborates on the use of YAML files to parse in the C# code. We have discussed a brief yet simple example to display the simple YAML file within the C# code, first using the serializer class function and then discussing the use of the deserializer library function to deserialize a YAML file into a simple line of text instead of converting it to the dictionary like format. This is about the parsing of the YAML file in the C# script.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.