using System.Collections.Generic;
using YamlDotNet.Serialization;
using YamlDotNet.RepresentationModel;
public static void Main()
ParseUsingRepresentationModel(yaml);
ParseUsingSerialization(yaml);
public static void ParseUsingRepresentationModel(string yaml)
Console.WriteLine("# ParseUsingRepresentationModel");
var stream = new YamlStream();
stream.Load(new StringReader(yaml));
var document = stream.Documents.First();
var rootMapping = (YamlMappingNode)document.RootNode;
var valuesMapping = (YamlMappingNode)rootMapping.Children[new YamlScalarNode("values")];
foreach(var tuple in valuesMapping.Children)
Console.WriteLine("{0} => {1}", tuple.Key, tuple.Value);
public static void ParseUsingSerialization(string yaml)
Console.WriteLine("# ParseUsingSerialization");
var deserializer = new Deserializer();
var result = deserializer.Deserialize<Dictionary<string, OrderPreservingDictionary<string, string>>>(new StringReader(yaml));
foreach(var tuple in result["values"])
Console.WriteLine("{0} => {1}", tuple.Key, tuple.Value);
public class OrderPreservingDictionary<TKey, TValue> : List<KeyValuePair<TKey, TValue>>, IDictionary<TKey, TValue>
public void Add(TKey key, TValue value)
Add(new KeyValuePair<TKey, TValue>(key, value));
public bool ContainsKey(TKey key)
throw new NotImplementedException();
public ICollection<TKey> Keys
get { throw new NotImplementedException(); }
public bool Remove(TKey key)
throw new NotImplementedException();
public bool TryGetValue(TKey key, out TValue value)
throw new NotImplementedException();
public ICollection<TValue> Values
get { throw new NotImplementedException(); }
public TValue this[TKey key]
return this.First(e => e.Key.Equals(key)).Value;