Click here to Skip to main content
15,175,959 members
Articles / Programming Languages / C#
Posted 20 Aug 2008

Tagged as


69 bookmarked

YAML Parser in C#

Rate me:
Please Sign up or sign in to vote.
4.83/5 (26 votes)
17 Feb 2011CPOL2 min read
An almost feature complete YAML parser.

Image 1

Image 2


YAML is a human-friendly, cross language, Unicode based data serialization language designed around the common native data types of agile programming languages. It is broadly useful for programming needs ranging from configuration files to Internet messaging to object persistence to data auditing.

Visit the official YAML website for more information.

YAML Basics

A YAML file may contain zero or more YAML documents, separated by document markers. A YAML document contains one root DataItem. There are three types of DataItems: Scalar, Sequence, and Mapping. DataItems may be nested to form structured data.

Each DataItem type has several formatting styles for good human readability.


Some rules:

  • A block style item can be nested to a block style item but not a flow style item.
  • A flow style item can be nested to either a block style or a flow style item.
  • Block structure is denoted by indentation.
  • All indentations only use space char, tab is not allowed.

Here are some examples:

Block Scalar

Literal Text
The text using
literal style.
The text using
literal style.
"The text using\nliteral style.\n""The text using\nliteral style."
Folded Text
The text using
folded style.
The text using
folded style.
"The text using folded style.\n""The text using folded style."
Flow Scalar
Plain Text
  • Can not start with ,[]{}#&*!|>'\"%@`
  • Can start with -?: followed by non space char
  • ": " and " #" cannot appear in between
'Single Quoted Text'
  • Line breaks are folded
  • "'" is escaped with "''"
"Double Quoted Text"
  • Line breaks are folded
  • Escape sequences can be used
Block Sequence
- Item one
- Item two
- Item three
Flow Sequence
[Item one, Item two,
Item three]
Block Mapping
Key1: Item one
Key2: Item two
? Key3
: Item three 
Flow Mapping
{Key1: Item one, Key2: Item two, 
Key3: Item three} 


Anchor and Alias
Key1: &items
  A: Item A
  B: Item B
Key2: *items
  A: Item A
  B: Item B
  A: Item A
  B: Item B
# whole line comment
Data Item # inline comment


There is already a Yaml Library for .NET project, but the features supported are limited.

Using the Code

The parser code is generated using a homemade tool based on grammar specified in the YAML.PEG.txt file. This grammar is not completely equal to the official YAML specification. Here are some differences:

A separator “,” is not allowed following the last entry of Sequence or Mapping in this parser. The 32-bit Unicode escape sequence “U” (ns-hex-digit × 8) is not supported.

The parser can be used like this:

YamlParser parser = new YamlParser();
TextInput input = new TextInput(File.ReadAllText(yamlFilePath));
bool success;
YamlStream yamlStream = parser.ParseYamlStream(input, out success);
if (success)
    foreach (YamlDocument doc in yamlStream.Documents)
        // access DataItem by doc.Root


YamlStream yamlStream = YamlParser.Load(yamlFilePath);

Points of Interest

The main shortcoming of this parser is that error messages are not intuitive. You are welcome to give suggestions.


  • 2008-08-21: Article submitted.
  • 2011-02-16: Redesigned the UI, fixed some parser bugs.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Liu Junfeng
Architect YunCheDa Hangzhou
China China
No Biography provided

Comments and Discussions

QuestionHow to access line comments and inline comments when parsing the structure Pin
Hanibani4215-Aug-15 11:51
MemberHanibani4215-Aug-15 11:51 
BugPossible NullReferenceException in places Pin
Austin Stephens20-Nov-13 14:58
MemberAustin Stephens20-Nov-13 14:58 
GeneralMy vote of 5 Pin
CaptainClueless6-Apr-13 13:07
MemberCaptainClueless6-Apr-13 13:07 
QuestionComplement to the YAML Parser Pin
CaptainClueless6-Apr-13 12:18
MemberCaptainClueless6-Apr-13 12:18 
AnswerRe: Complement to the YAML Parser Pin
CaptainClueless6-Apr-13 13:15
MemberCaptainClueless6-Apr-13 13:15 
QuestionHow do I get the data out of the tree? Pin
CaptainClueless6-Apr-13 11:54
MemberCaptainClueless6-Apr-13 11:54 
QuestionNew to YAML Pin
Hanibani27-Sep-12 7:03
MemberHanibani27-Sep-12 7:03 
Question谢谢你! Pin
sebgod28-Dec-11 22:37
Membersebgod28-Dec-11 22:37 
SuggestionVery good Library Pin
Madhur Ahuja30-Aug-11 1:50
MemberMadhur Ahuja30-Aug-11 1:50 
GeneralCould you release this under GPL, LGPL, or BSD licence please? Pin
aj_codeproject26-May-11 4:43
Memberaj_codeproject26-May-11 4:43 
GeneralRe: Could you release this under GPL, LGPL, or BSD licence please? Pin
Member 393683426-May-11 6:26
MemberMember 393683426-May-11 6:26 
GeneralRe: Could you release this under GPL, LGPL, or BSD licence please? Pin
Liu Junfeng4-Jun-11 3:02
MemberLiu Junfeng4-Jun-11 3:02 
GeneralRe: Could you release this under GPL, LGPL, or BSD licence please? Pin
aj_codeproject22-Jun-11 16:17
Memberaj_codeproject22-Jun-11 16:17 
GeneralNearly Perfect [modified] Pin
Mercury Phlegmatic20-Mar-11 10:57
MemberMercury Phlegmatic20-Mar-11 10:57 
GeneralThis parser seems good... Pin
aamironline18-Feb-11 2:39
Memberaamironline18-Feb-11 2:39 
QuestionProblem with folded text scalar Pin
Anders Jørgensen20-Jan-11 3:40
MemberAnders Jørgensen20-Jan-11 3:40 
AnswerRe: Problem with folded text scalar Pin
Anders Jørgensen20-Jan-11 4:24
MemberAnders Jørgensen20-Jan-11 4:24 
AnswerRe: Problem with folded text scalar Pin
Liu Junfeng17-Feb-11 15:48
MemberLiu Junfeng17-Feb-11 15:48 
GeneralRe: Problem with folded text scalar Pin
Anders Jørgensen17-Feb-11 21:43
MemberAnders Jørgensen17-Feb-11 21:43 
GeneralNew to YAML Pin
rko10020-Aug-10 3:42
Memberrko10020-Aug-10 3:42 
GeneralNested sequences not working Pin
Member 293651324-Mar-10 13:35
MemberMember 293651324-Mar-10 13:35 
GeneralRe: Nested sequences not working Pin
Liu Junfeng20-Jul-10 1:44
MemberLiu Junfeng20-Jul-10 1:44 
GeneralWell done. Pin
CooperWu30-Jul-09 20:50
MemberCooperWu30-Jul-09 20:50 
GeneralDoesn't work with non-indent list Pin
sgwong15-Jul-09 18:38
Membersgwong15-Jul-09 18:38 
GeneralRe: Doesn't work with non-indent list Pin
Liu Junfeng19-Jul-09 22:59
MemberLiu Junfeng19-Jul-09 22:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.