Create an anonymous type object from an arbitrary text file

Posted by Robert Harvey on Stack Overflow See other posts from Stack Overflow or by Robert Harvey
Published on 2010-05-04T05:03:00Z Indexed on 2010/05/04 5:08 UTC
Read the original article Hit count: 412

I need a sensible way to draw arbitrary text files into a C# program, and produce an arbitrary anonymous type object, or perhaps a composite dictionary of some sort.

I have a representative text file that looks like this:

adapter 1: LPe11002
  Factory IEEE: 10000000 C97A83FC
  Non-Volatile WWPN: 10000000 C93D6A8A , WWNN: 20000000 C93D6A8A
adapter 2: LPe11002
  Factory IEEE: 10000000 C97A83FD
  Non-Volatile WWPN: 10000000 C93D6A8B , WWNN: 20000000 C93D6A8B

Is there a way to get this information into an anonymous type object or some similar structure?

The final anonymous type might look something like this, if it were composed in C# by hand:

new
{
    adapter1 = new 
    { 
        FactoryIEEE = "10000000 C97A83FC",
        Non-VolatileWWPN = "10000000 C93D6A8A",
        WWNN = "20000000 C93D6A8A"
    }
    adapter2 = new 
    { 
        FactoryIEEE = "10000000 C97A83FD",
        Non-VolatileWWPN = "10000000 C93D6A8B",
        WWNN = "20000000 C93D6A8B"
    }
}

Note that, as the text file's content is arbitrary (i.e. the keys could be anything), a specialized solution (e.g. that looks for names like "FactoryIEEE") won't work. However, the structure of the file will always be the same (i.e. indentation for groups, colons and commas as delimiters, etc).

Or maybe I'm going about this the wrong way, and you have a better idea?

© Stack Overflow or respective owner

Related posts about c#

Related posts about anonymous-types