using YamlDotNet.Serialization;
using System.Collections.Generic;
using YamlDotNet.RepresentationModel;
public static void Main()
using (var r = new StringReader(Program.Document))
yamlObject = new Deserializer().Deserialize(r);
var data = new YamlQuery(yamlObject)
Console.WriteLine("all names of pods");
Console.WriteLine(string.Join(",", data));
data = new YamlQuery(yamlObject)
Console.WriteLine("all names of pods in ressources");
Console.WriteLine(string.Join(",", data));
public YamlQuery(object yamlDic)
public YamlQuery On(string key)
this.current = query<object>(this.current ?? this.yamlDic, this.key, null);
public YamlQuery Get(string prop)
if (this.current == null)
throw new InvalidOperationException();
this.current = query<object>(this.current, null, prop, this.key);
public List<T> ToList<T>()
if (this.current == null)
throw new InvalidOperationException();
return (this.current as List<object>).Cast<T>().ToList();
private void _query(YamlStream yaml)
(YamlMappingNode)yaml.Documents[0].RootNode;
private IEnumerable<T> query<T>(object _dic, string key, string prop, string fromKey = null)
var result = new List<T>();
if (typeof(IDictionary<object, object>).IsAssignableFrom(_dic.GetType()))
var dic = (IDictionary<object, object>)_dic;
var d = dic.Cast<KeyValuePair<object, object>>();
if (dd.Key as string == key)
result.AddRange(query<T>(dd.Value, key, prop, dd.Key as string));
else if (fromKey == key && dd.Key as string == prop)
result.AddRange(query<T>(dd.Value, key, prop, dd.Key as string));
else if (typeof(IEnumerable<object>).IsAssignableFrom(_dic.GetType()))
var t = (IEnumerable<object>)_dic;
result.AddRange(query<T>(tt, key, prop, key));
private const string Document = @"---
receipt: Oz-Ware Purchase Invoice
descrip: Water Bucket (Filled)
descrip: High Heeled ""Ruby"" Slippers
descrip: High Heeled ""Ruby"" Slippers
descrip: High Heeled ""Ruby"" Slippers
descrip: High Heeled ""Ruby"" Slippers
descrip: High Heeled ""Ruby"" Slippers
Road to the Emerald City.