public static RootContext ReadString(string s) { string[] lines = s.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None); RootContext root = new RootContext(); Category c = null; foreach (string line in lines) { string thisLine = line.Trim(); if (thisLine == String.Empty) { continue; } string name, value; if (ParseCategory(thisLine, out name)) { c = new Category() { Name = name }; root.Categories.Add(c); } else if (ParseValue(thisLine, out name, out value)) { Option o = new Option() { Name = name, Value = value, Category = c }; if (c != null) { c.Options.Add(o); } else { root.Options.Add(o); } } else { // do some logging here } } return root; }
private static string FormatCategory(Category c) { string s = String.Format("[{0}]{1}", c.Name, System.Environment.NewLine); foreach (Option o in c.Options) { s += FormatOption(o); } return s; }