How to use YAML file to read configuration settings

Jan 24, 2010 at 1:05 PM

Very much like in Ruby, I would like to use this library to read application configuration settings from a YAML file.

Can you show an example C# code which allows reading the YAML file and allows getting the config values based on keys? Which minimum version of .NET framework I need to perform this task? Does YAML lib support arrays like in the below example? 'connections' is an array of 'connection' items.

Here is sample of what I want to read:




name: Connection1



name: Connection2




name: queue1


name: queue2

Jan 25, 2010 at 9:01 AM
Edited Jan 25, 2010 at 9:02 AM

Hello Nokiola, and welcome

The API for deserializing into objects isn't firm yet, but not far off.   I have done several experiments, but haven't come up with one as flexible as I really want.  My library should be compatible back to .NET 2.0.   I'm using .NET 3.5 in some of the tests, but I don't think thats needed by the core library.

For you example, what Types do you want the results returned as?  As C# classes like Production containing two public members consisting of arrays named connectionss & queues, where those are additional classes, or are you looking for something more dynamic language style where these all get read into instances of the Hashtable and Array classes (or something similar)?

When typing YAML examples its best to use the Insert Code Snipet button (</>) so that the spacing is preserved.   You mentioned arrays, for that to work you would need to use YAML sequence notation with my package.   Here's what it might look like:

%YAML 1.2
  - name: Connection1
  - name: Connection2
  - queue1
  - queue2



Jan 26, 2010 at 3:30 PM

I do prefer deserialization as a class instead of hashtables... I don't mind for .net 3.5, but definitely .net 4.0 is not an option yet.

XmlSerializer is an good example of "API" from my point of view. It is easy and quick to transform to xml and back a .NET object.


Feb 6, 2010 at 7:11 PM

I am curious to know how things are going with the serializer work?  I was considering taking a stab at it.  Are you looking for contributions?

I was thinking of a few ways to get the data out. 

First, is a direct mapping.  If I have an object tree of hashes and collections (of any type), I would populate them by name.  If they don't name and type, serialization fails.

Second, is an attribute-based approach.  Kind of like the XmlSerializer works.  If I want the names to map into my object tree differently, I have attribute control.

Lastly is an optional DynamicObject (.Net 4.0) base approach.   This can just be a wrapper around the existing serialization technique.  It would be very simple, actually, to do it that way, I think.  I did something similar that wraps the XDocument (Linq2Xml document) nodes with a DynamicObject node, and use the dynamic variables in C# to make it work.  This approach would feel very Ruby-ish.

Just some thoughts.