3

I have a CSV and some of the headers have spaces or symbols in, eg. Country Name or Post/Zip Code

I'm using some code like this to import it by mapping it to CatRecord objects:

FileHelperEngine<CatRecord> engine = new FileHelperEngine<CatRecord>();  
engine.Options.IgnoreFirstLines = 1;      
var files = Directory.GetFiles(filePath);  
var catRecords = engine.ReadFile(files.Single());

I create a field in my class but I need to tell it to map it to "Country Name".

I found another question that said use [FieldTitle] however no such attribute exists in the FileHelpers namespace. I'm using the latest one via NuGet (version 2 I think).

Any ideas?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
NibblyPig
  • 51,118
  • 72
  • 200
  • 356

1 Answers1

3

I think you are missing something. The FileHelpers engine does not care about the headers because you have told it to ignore them with IgnoreFirstLines = 1.

It will map the first CSV column it finds to the first public field in CatRecord. The second column to the second field, etc. The field names do not have to match the headers in your CSV file. (You could call them Field1, Field2, etc., if you prefer).

shamp00
  • 11,106
  • 4
  • 38
  • 81