1

I'm trying to read a csv file using LingToCSV, containing decimal values with a comma a seperator instead of a point.

This is one row of input:

"DataField1","DataField2"
"Test", "0,34"

And I use this class to read it:

public class Record
{
   [CsvColumn(Name = "DataField1", FieldIndex=  1)]
   string DataField1 {get;set;}

   [CsvColumn(Name = "DataField2", FieldIndex=  2, OutputFormat="C", NumberStyle=NumberStyles.Currency)]
   decimal DataField2 {get;set;}
}

However, this doesn't work and generates this exception:

{"Value \"DataField2\" in line 9 has the wrong format for field or property \"DataFoe;d2\" in type \"Record\"."}

Obviously it's expecting a point, but how can I make it work using the commas.

Timo Willemsen
  • 8,717
  • 9
  • 51
  • 82
  • 1
    Can we propose a solution without LinqToCsv? – Marco May 28 '14 at 11:49
  • Well, I appreciate you're helping me problem solving without using a specific technique. But we need to write a lot of CSV parsers (to link our application to a lot of different providers). And I was experimenting with LinqToCSV because it is so easy to use and quick to write, and I was wondering if it was possible using LinqToCSV – Timo Willemsen May 28 '14 at 11:52
  • Is "0,34" an appropriate value for a currency type? I'd try some other values there. Is that a typo? Or, should it be "0.34". – L_7337 May 28 '14 at 12:00
  • It is in the netherlands, so that's kinda my question. – Timo Willemsen May 28 '14 at 12:01
  • You probably have to set some sort of currency culture format. – L_7337 May 28 '14 at 12:02
  • What bugs me, is that the error message says "in Line 9". Is the format of the values before Line 9 and after Line 9 different? – Marco May 28 '14 at 12:04

1 Answers1

4

Believe you need to set the FileCultureName in the CsvFileDescription...

CsvFileDescription fileDesc = new CsvFileDescription
        {

            FirstLineHasColumnNames = true
            FileCultureName = "nl" 
        };
dbugger
  • 15,868
  • 9
  • 31
  • 33