// Collection of code snippets by Arne Vajhøj // (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) using System; using System.Collections.Generic; using System.IO; using YamlDotNet.Serialization; using YamlDotNet.Core.Events; namespace YamlProc.TreeSmart { public class TypeDetector : INodeTypeResolver { public bool Resolve(NodeEvent evt, ref Type typ) { if(evt is Scalar) { string s = ((Scalar)evt).Value; int iv; if(int.TryParse(s, out iv)) { typ = typeof(int); return true; } double xv; if(double.TryParse(s, out xv)) { typ = typeof(double); return true; } } return false; } } public class Program { public static void Main(string[] args) { IDeserializer dser = (new DeserializerBuilder()).WithNodeTypeResolver(new TypeDetector()).Build(); IDictionary cfg = (IDictionary)dser.Deserialize(new StreamReader(@"C:\Work\demo1.yaml")); IDictionary ab = (IDictionary)cfg["ab"]; IDictionary de = (IDictionary)cfg["de"]; int a = (int)ab["a"]; int b = (int)ab["b"]; int c = (int)cfg["c"]; string d = (string)de["d"]; string e = (string)de["e"]; string f = (string)cfg["f"]; Console.WriteLine("{0} {1} {2} {3} {4} {5}", a, b, c, d, e, f); } } }