Question

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
Was it helpful?

Solution

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)

OTHER TIPS

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.

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top