I'm using CsvHelper v26.1.0 to read the following text file delimited by ~:
123~John
234~Joe "Public"
But the double quotes in the file is causing CsvHelper to treat them as bad data. I tested it by removing the double quotes and it worked fine. But the question is, I already set a custom delimiter, why is the double quote still causing the problem?
public class AccountDtoMap : ClassMap<AccountDto>
{
public AccountDtoMap()
{
Map(m => m.Number).Index(0);
Map(m => m.Name).Index(1);
}
}
var cfg = new CsvHelper.Configuration.CsvConfiguration(CultureInfo.InvariantCulture)
{
Delimiter = "~",
HasHeaderRecord = false,
MissingFieldFound = (context) => { errs.Add($"{typeof(T)} missing field: {context.Context.Parser.RawRecord}"); },
BadDataFound = (context) => { errs.Add($"{typeof(T)} bad data: {context.RawRecord}"); },
};
using (var csv = new CsvReader(new StreamReader(file), cfg))
{
csv.Context.RegisterClassMap<AccountDtoMap>();
return csv.GetRecords<T>().ToList();
}
Runnable demo here.