tags:

views:

220

answers:

4

I have this exported file of some weird (standard for this industry!) format, which I need to import into our Database. The file basically looks like this:

DATRKAKT-START
  KAKT_LKZ  "D"
  KAKT_DAT_STAMM    "1042665"

  DATRKAIB-START
    KAIB_AZ "18831025"
    KAIB_STATUS_FM  2
    KAIB_KZ_WAE "E"
  DATRKAIB-END

  DATRKARP-START
    KARP_MELD   "831025"
    KARP_ST_MELD    "G"
  DATRKARP-END

...
DATRKAKT-END

There are 56 sections with a total of 1963 different rows, so I'm really not into creating 56 classes with 1963 properties... How would you handle this file so that you can access some property like it were an object?

Datrkaib.Kaib_Status_Fm
Datrkarp.karp_St_Meld
A: 

I'd go with a List <name, list> of various object, that can be a tuple <name, value> or a named list of object.

akappa
+1  A: 

It looks like structured data - I'd run search and replace and convert it to a simple xml. and then import.

The if you want to generate a code file off of it - consider codesmith - I think it can do this.

Preet Sangha
My inclination is the exact opposite. The data is already well-structured. Why bother with a conversion?
Daniel Straight
Because search and replace is much simpler than writing a parser for that format, because in .NET you get XML reading, transformations and serialization for free.
OregonGhost
I really don't know why they don't provide this export as xml aswell... would be that easy. Reparsing it to xml before importing really was my first idea, but somehow I managed to forget about it.Did I mention how much I hate the specification for this format?...
Markus Nigbur
A: 

There isn't that will automatically do this for you.

I would create a class containing all the appropriate properties (say DatrDocument), and create a DatrReader class (similar idea to the XmlDocument/XmlReader classes).

The DatrReader will need to read the contents of the file or stream, and parse it into a DatrDocument.

You may also want to write a DatrWriter class which will take a DatrDocument and write it to a stream.

Rodney Richardson
+1  A: 

Unless your programming language lets you add methods to classes at runtime or lets classes respond to calls to undefined methods, there's no way you can do this. The thing is, even if C# did let you do this, you would lose type safety and Intellisense help anyway (presumably among the reasons for wanting it to work like that), so why not just go ahead and read it into some data structure? My inclination would be a hash which can contain values or other hashes, so you'd get calls like (VB):

Datrkakt("Lkz")
Datrkakt("Dat_Stam")
Datrkakt("Kaib")("Az")
Datrkakt("Kaib")("Status_Fm")

Or if you know all the data items are uniquely named as in your example, just use one hash:

Datr("Kakt_Lkz")
Datr("Kakt_Dat_Stam")
Datr("Kaib_Az")
Datr("Kaib_Status_Fm")

You can get back Intellisense help by creating an enum of all the data item names and getting:

Datr(DatrItem.KAKT_LKZ)
Datr(DatrIrem.KAIB_STATUS_FM)
Daniel Straight
I forgot to mention each section can exists x times..So even Datrkakt("Kaib")("Status_Fm") would give me a hard time.Still, what I'm doing right now is similiar to your approach.I'll probably accept it as the answere if noone comes up with an easier approach.
Markus Nigbur