When using the CsvHelper library and in particular the CsvReader.Read()
function, is there a way to ignore blank records and/or whitespace?
I need to return the raw string[]
but was hoping, I could do some cleanup functions when parsing with the library.
I've checked Github and CsvReader.Read()
seems to use SkipEmptyRecords but this doesn't work for me as I have whitespace.
Here's my csv file, its encoded in UTF8 without BOM.
I've tried ASCII encoding, just in case I missed something but that didn't work either.
If no one knows I'll chat to Josh and submit a git request with a fix.
Reference: http://joshclose.github.io/CsvHelper/
I think your best bet is to fix the library yourself.
The problem is that the
Read
method uses theSkipEmptyRecords
setting and theIsRecordEmpty
method to determine if it should skip a field or not:However the IsRecordEmpty() method is not perfectly implemented to support your scenario, because it uses the following code:
That does not work because your strings are not null or empty. In my opinion combining Trimming with SkipEmptyRecords could work in theory:
But again the trimming is not utilized when checking if the field is empty or not, so i am pretty sure your only option is to fix the library yourself and to use the trimming in the IsRecordEmpty() method OR use IsNullOrWhiteSpace instead of IsNullOrEmpty.