Search code examples
c#fileparse

Best method of Textfile Parsing in C#?


I want to parse a config file sorta thing, like so:

[KEY:Value]     
    [SUBKEY:SubValue]

Now I started with a StreamReader, converting lines into character arrays, when I figured there's gotta be a better way. So I ask you, humble reader, to help me.

One restriction is that it has to work in a Linux/Mono environment (1.2.6 to be exact). I don't have the latest 2.0 release (of Mono), so try to restrict language features to C# 2.0 or C# 1.0.


Solution

  • I considered it, but I'm not going to use XML. I am going to be writing this stuff by hand, and hand editing XML makes my brain hurt. :')

    Have you looked at YAML?

    You get the benefits of XML without all the pain and suffering. It's used extensively in the ruby community for things like config files, pre-prepared database data, etc

    here's an example

    customer:
      name: Orion
      age: 26
      addresses:
        - type: Work
          number: 12
          street: Bob Street
        - type: Home
          number: 15
          street: Secret Road
    

    There appears to be a C# library here, which I haven't used personally, but yaml is pretty simple, so "how hard can it be?" :-)

    I'd say it's preferable to inventing your own ad-hoc format (and dealing with parser bugs)