I am using CsvHelper library to parse CSV data to C# object. So far, I can parse all this class with these 3 columns.
public class Foo
{
public string Id { get; set; }
public decimal Amount { get; set; }
public string CurrencyCode { get; set; }
}
But after I added the DateTime property, it breaks. Class after added new column.
public class Foo
{
public string Id { get; set; }
public decimal Amount { get; set; }
public string CurrencyCode { get; set; }
public DateTime TransactionDate { get; set; }
}
This is my configuration
var config = new CsvConfiguration(CultureInfo.InvariantCulture)
{
HasHeaderRecord = false,
};
using (var reader = new StreamReader(filePath))
using (var csv = new CsvReader(reader, config))
{
csv.Context.RegisterClassMap<CsvMap>();
var records = csv.GetRecords<Foo>();
foreach (var record in records)
{
Console.WriteLine(record.Id);
}
}
public sealed class CsvMap : CsvHelper.Configuration.ClassMap<Foo>
{
public CsvMap()
{
string format = "yyyy-MM-dd hh:mm:ss";
var msMY = CultureInfo.GetCultureInfo("ms-MY");
Map(m => m.TransactionDate).TypeConverterOption.Format(format)
.TypeConverterOption.CultureInfo(msMY);
}
}
and this is my data
"Invoice0000001","1,000.00","USD","20/02/2019 12:33:16"
"Invoice0000002","300.00","USD","21/02/2019 02:04:59"
Note: The date format in the above date is DD/MM/YYYY HH:MM:SS
