I have a CSV file with a record count row at the end
id|key|limit
123|1|591
456|2|921
record_count|2
When I run this with CsvHelper
using a class map it throws an exception when it gets to the record_count|2
row. I'm working around it by configuring a ReadingExceptionOccurred
handler to ignore that row.
csvConfig.ReadingExceptionOccurred = ex =>
{
if (ex.Exception.Context.Parser.RawRecord.Contains("record_count"))
{
return false;
}
return true;
};
This works but is there is a more "standard" method for handling this footer record?
You could use ShouldSkipRecord
in the configuration.
void Main()
{
var config = new CsvConfiguration(CultureInfo.InvariantCulture)
{
Delimiter = "|",
ShouldSkipRecord = args => args.Record[0] == "record_count"
};
using (var reader = new StringReader("id|key|limit\n123|1|591\n456|2|921\nrecord_count|2"))
using (var csv = new CsvReader(reader, config))
{
var records = csv.GetRecords<Foo>().Dump();
}
}
public class Foo
{
public int id { get; set; }
public int key { get; set; }
public int limit { get; set; }
}