FileHelpers dynamic fixed field positions

user3086298

Is it possible to parse/read a fixed position layout flat file when the positions are only known at run time? I see there is a question on SO that relates to something similar using 'Runtime Records' but this relates reading a delimited file - Dynamically create a Fixed Length text file with FileHelpers

My intention is to parse fixed length flat files with different fixed length formats to a common format, and the fixed length format is loaded at runtime. FileHelpers tends to use attributes to define the position and length of the field, which is defined up front but I would like to specify this at runtime.

thanks.

Manoj

This is what i did. Hope it may help you.

  1. Created DB table and stored the different Fixed File layout formats (like Field Name size, Data Type and Length of the field etc...)
  2. In UI first user selects the Fixed File Layout and then selects the Fixed File to import.
  3. Based on selected Layout i create Fixed length Type class at run time using the below code.

private void ImportFiles() { DataTable dt = GetFixedFileSpecsFromDB(layoutName); //Get the specs //Create Dynamic class based on Specs above FixedLengthClassBuilder cb = GetSpecClass(dt); //Create FileHelpers engine instance FileHelperEngine engine = new FileHelperEngine(cb.CreateRecordClass()); //Read file data into Data table DataTable dtImportData = engine.ReadFileAsDT(ImportFilePath); } //Method to create the Fixed lentgh dynamic class public static FixedLengthClassBuilder GetSpecClass(DataTable dt) { string className = "ImportSpecifications"; FixedLengthClassBuilder cb = new FixedLengthClassBuilder(className); //Loop thru each field and prepare the class foreach (DataRow dr in dt.Rows) { int fieldLength = Convert.ToInt32(dr.Field<decimal>("FieldLength")); switch (dr["FieldDataType"].ToString()) { case "String": cb.AddField(dr.Field<string>("FieldName"), fieldLength, typeof(string)); cb.LastField.FieldNullValue = string.Empty; cb.LastField.TrimMode = FileHelpers.TrimMode.Both; break; case "Date": cb.AddField(dr.Field<string>("FieldName"), fieldLength, typeof(DateTime)); cb.LastField.FieldNullValue = string.Empty; break; case "Integer": cb.AddField(dr.Field<string>("FieldName"), fieldLength, typeof(int?)); //cb.LastField.FieldNullValue = 0; break; case "Long Integer": cb.AddField(dr.Field<string>("FieldName"), fieldLength, typeof(long)); cb.LastField.FieldNullValue = 0; break; case "Decimal": cb.AddField(dr.Field<string>("FieldName"), fieldLength, typeof(decimal?)); //cb.LastField.FieldNullValue = 0; break; default: break; } } return cb; }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

FileHelpers dynamic fixed field positions

From Dev

Using FileHelpers.Dynamic, read a fixed-width file and upload to SQL

From Dev

Filehelpers Field max length

From Dev

FileHelpers: mixing Delimited and Fixed Length Records

From Dev

How to map a dynamic JSON property to a fixed POJO field

From Dev

How to map a dynamic JSON property to a fixed POJO field

From Dev

Filehelpers - Export with double quotes around each field

From Dev

FileHelpers: The delimiter '|' can´t be found after the field

From Dev

Graph Planarity with Fixed Node Positions

From Dev

string replacement of fixed positions in python

From Dev

fixed positions filling up div

From Dev

Linkedin API - Positions field

From Dev

Dynamic quadrant label positions with Highcharts

From Dev

Ignore dollar currency sign ($) in decimal field with Filehelpers library

From Dev

FileHelpers library - Is it possible to change field's date format at runtime?

From Dev

MySQL result set ordered by fixed positions

From Dev

dynamic and fixed width with css

From Dev

Dynamic GridLayout with fixed size

From Dev

Overlapping dynamic fixed header

From Dev

Fixed header, fixed footer dynamic content

From Dev

Dynamic programming - fixed sum of fixed size array

From Dev

Filehelpers - Export with double quotes around each field EXCEPT when field is blank/null

From Dev

Filehelpers - Export with double quotes around each field EXCEPT when field is blank/null

From Dev

Create array positions as dynamic array in matlab

From Dev

Fixed a header with a FragmentList and with dynamic contents

From Dev

UITableView fixed section with dynamic size

From Dev

C (Dynamic) Array (Fixed size)

From Dev

Dynamic parsing of fixed text files

From Dev

Regex for numbers with optional decimal value, fixed to two positions?

Related Related

  1. 1

    FileHelpers dynamic fixed field positions

  2. 2

    Using FileHelpers.Dynamic, read a fixed-width file and upload to SQL

  3. 3

    Filehelpers Field max length

  4. 4

    FileHelpers: mixing Delimited and Fixed Length Records

  5. 5

    How to map a dynamic JSON property to a fixed POJO field

  6. 6

    How to map a dynamic JSON property to a fixed POJO field

  7. 7

    Filehelpers - Export with double quotes around each field

  8. 8

    FileHelpers: The delimiter '|' can´t be found after the field

  9. 9

    Graph Planarity with Fixed Node Positions

  10. 10

    string replacement of fixed positions in python

  11. 11

    fixed positions filling up div

  12. 12

    Linkedin API - Positions field

  13. 13

    Dynamic quadrant label positions with Highcharts

  14. 14

    Ignore dollar currency sign ($) in decimal field with Filehelpers library

  15. 15

    FileHelpers library - Is it possible to change field's date format at runtime?

  16. 16

    MySQL result set ordered by fixed positions

  17. 17

    dynamic and fixed width with css

  18. 18

    Dynamic GridLayout with fixed size

  19. 19

    Overlapping dynamic fixed header

  20. 20

    Fixed header, fixed footer dynamic content

  21. 21

    Dynamic programming - fixed sum of fixed size array

  22. 22

    Filehelpers - Export with double quotes around each field EXCEPT when field is blank/null

  23. 23

    Filehelpers - Export with double quotes around each field EXCEPT when field is blank/null

  24. 24

    Create array positions as dynamic array in matlab

  25. 25

    Fixed a header with a FragmentList and with dynamic contents

  26. 26

    UITableView fixed section with dynamic size

  27. 27

    C (Dynamic) Array (Fixed size)

  28. 28

    Dynamic parsing of fixed text files

  29. 29

    Regex for numbers with optional decimal value, fixed to two positions?

HotTag

Archive