Search code examples
.nettemplate-engineflat-file

What is the best way to generate flat files from templates, and parse them back


Here's the problem I have, I need to generate a flat string file with a rather complex (imposed) structure based on field length and start and stop positions. It file will be generated from a .Net application (data stored in SQL Server). It has different headers with different templates. The structure might also change overtime. The same type of file will also have to be parsed back into my system.

What I would preferably like is creating a template that defines the look of the file, for example with the following attribute: Name, Type, Field Length, Start & End Position, Default value.

And be able to generate the file from some kind of view data and then to parse it back from the same template.

I'm pretty sure I'm not the first one to have that kind of trouble, but I cannot find a good library on the Internet. I've looked at StringTemplate but it doesn't seem to be able to create templates based on length and position of data.

Thanks!


Solution

  • I don't know about a generic component which deals with this. But I'd write a generic tool which based on the template definition uses reflection to fill properties in an object.

    Your 'template' would need to define the structure of the file, as you already described and the full name of the class you want to load the data into (and perhaps the assembly containing the class if that can change).

    The basic flow would be:

    • Check header for correctness (optional)
    • Loop though the data lines
      • Create new instance of target class (use Assembly.GetType() andType.GetConstructor()`)
      • Loop through the fields
        • Parse value according to their type
        • Set the value of the property with the same name (using Type.GetProperty() and PropertyInfo.SetValue())
      • Add the object to a result collection.
    • Done

    Just make sure your view objects have a default constructor and all the required properties and you should be fine.

    Writing the file can be done much the same way using reflection to get the values of your view object.