I have a TXT file to parse and I’m using the CsvHelper package to do so. The content of the file is:
A; B; C; D; E; F; G; H
a;b;;2021-05-06;e;11:00;3;9
Note there’re spaces on the header line, before the column names ( A
, B
, etc.).
I’m using the following class to parse the CSV:
public class MyClass
{
[Name(" A")]
public string a { get; set; }
[Name(" B")]
public string b { get; set; }
[Name(" C")]
public string c { get; set; }
[Name(" D")]
public DateTime d { get; set; }
[Name(" E")]
public string e { get; set; }
[Name(" F")]
public TimeSpan f { get; set; }
[Name(" G")]
public string g { get; set; }
[Name(" H")]
public string h { get; set; }
}
and the following logic for the deserialization:
var reader = new CsvReader(myStreamReader, new CsvConfiguration(CultureInfo.InvariantCulture)
{
Delimiter = ";",
MissingFieldFound = null
});
reader.GetRecords<MyClass>().ToList();
My problem is that I get the following error at runtime:
It seems to say it isn’t able to understand the 2021-05-06
date, but not because the format is not valid, instead the error shows that Text: ''
, while I’d expect Text: '2021-05-06'
. I tried using the [Format("yyyy-MM-dd")]
attribute like said here, but without success (maybe because it’s only for writing, not reading).