-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Open
Labels
Description
Describe the bug
When using CsvConfiguration.ShouldSkipRecord, CsvReader.GetRecords may skip the header row.
To Reproduce
public class CsvData
{
public string Name { get; set; }
}
var csvContent = "Name\nArnold\nAndrew\nBetty\nFrank";
using var stream = new MemoryStream(Encoding.UTF8.GetBytes(csvContent));
using var reader = new StreamReader(stream);
var configuration = new CsvConfiguration(CultureInfo.InvariantCulture)
{
ShouldSkipRecord = shouldSkipRecordArgs => !shouldSkipRecordArgs.Row[0].StartsWith("A")
};
var csvReader = new CsvReader(reader, configuration);
var records = csvReader.GetRecords<CsvData>().ToList();
Expected behavior
GetRecords should return two records, Arnold and Andrew. However, the header row is skipped and "Arnold" becomes the header. Then a HeaderValidationException is thrown for missing header with name "Name". ShouldSkipRecord should not apply to the header row.
Additional context
If I add a ClassMap to map index 0 to the Name property, I won't get the HeaderValidationException, but the GetRecords will only return one record, Andrew.
class CsvDataMap : ClassMap<CsvData>
{
public CsvDataMap()
{
Map(m => m.Name).Index(0);
}
}
csvReader.Context.RegisterClassMap<CsvDataMap>();