25650113-how-to-parse-a-yaml-string

CSharp · Console

/Program.cs

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using YamlDotNet.Serialization;

public class Program
{
	public static void Main()
	{
		var yaml = @"
- Label: entry
  Layer: x
  id: B35E246039E1CB70
- Ref: B35E246039E1CB70
  Label: Info
  Layer: x
  id: CE0BEFC7022283A6
- Ref: CE0BEFC7022283A6
  Label: entry
  Layer: HttpWebRequest
  id: 6DAA24FF5B777506
";
		
		var deserializer = new Deserializer();
		var result = deserializer.Deserialize<List<Hashtable>>(new StringReader(yaml));
		foreach (var item in result)
		{
			Console.WriteLine("Item:");
			foreach (DictionaryEntry entry in item)
			{
				Console.WriteLine("- {0} = {1}", entry.Key, entry.Value);
			}
		}
	}
}
;