6

Trying to read a CSV file that has empty rows (usually at the end) using CsvHelper.GetRecords<T>().

Without the empty rows this works a treat. However if the CSV file has an empty row (defined as , , , , , ) then it throws a TypeConverterException

Text: ''
MemberType: IntelligentEditing.PerfectIt.Core.DataTypes.Styles.StyleRuleType
TypeConverter: 'CsvHelper.TypeConversion.EnumConverter'

I have gone through the documentation (https://joshclose.github.io/CsvHelper/api/CsvHelper.Configuration/Configuration/) and have tried setting up the configuration object to IgnoreBlankLines = true however this has not worked.

Simplified for an example:

public enum ItemTypeEnum
{
    Unknown = 0,
    Accounts = 1,
    HR = 2,
}


public class CsvItemDto
{
    public int Id { get; set; }

    public string Value { get; set; }

    public ItemTypeEnum ItemType { get; set; }
}

.
.
.
var configuration = new Configuration()
{
    HasHeaderRecord = true,
    HeaderValidated = null,
    MissingFieldFound = null,
    IgnoreBlankLines = true,

};
var csv = new CsvReader(textReader, configuration);
var rows = csv.GetRecords<CsvItemDto>();


if (rows != null)
{
    var items = rows.ToList();
    //Throws exception here
}

The CSV would usually contain something like this:

Id,Value,ItemType
1,This,Unknown
2,That,Accounts
3,Other,HR
,,
,,

I expected the IgnoreBlankLines to ignore the blank rows in the CSV but it is not. Any ideas?

2
  • These lines with only a dot will be interpreted as being the first column, which is numeric input. No matter how you twist that, for a bare integer column, that is illegal input. If these were only put there as vertical '...' ellipsis, please leave them out of the example.
    – Nyerguds
    Sep 18, 2019 at 10:56
  • @Nyerguds apologies I should have made it clear that the dots were other rows that I haven't been bothered to type. Ill remove them for clarity.Cheers
    – Rhys Hicks
    Sep 18, 2019 at 12:49

4 Answers 4

11

@phat.huynh has the right idea. Just tell it to skip any record where all the fields are empty strings.

var configuration = new Configuration()
{
    HasHeaderRecord = true,
    HeaderValidated = null,
    MissingFieldFound = null,
    ShouldSkipRecord = record => record.All(field => String.IsNullOrWhiteSpace(field))
};
1
  • 3
    I'd suggest to use string.IsNullOrWhiteSpace(field) which will cover empty as well as other invisible characters that might be in incoming CSV stream
    – Sharif
    Sep 9, 2020 at 19:30
8

In addition to @David Specht's answer. The newer version has updated delegate ShouldSkipRecord. I am using version 28.0.1, the code below works for me.

ShouldSkipRecord = args => args.Row.Parser.Record.All(string.IsNullOrWhiteSpace)
2

you can try to implement ShouldSkipRecord on Configuration to choose skip or not

var configuration = new Configuration () {
                HasHeaderRecord = true,
                HeaderValidated = null,
                MissingFieldFound = null,
                IgnoreBlankLines = true,
                ShouldSkipRecord = (records) =>
                {
                    // Implement logic here
                    return false;
                }
            };
1
  • I did consider that I just assumed that this problem is straightforward configuration boolean setting. If no other answers appear I will probably go with this answer
    – Rhys Hicks
    Sep 18, 2019 at 12:51
1

An alternative way, in CsvHelper 23.0.0, is managing the reader exceptions

var conf = new CsvConfiguration(new CultureInfo("en-US"));
conf.ReadingExceptionOccurred = (exc) =>
{
    Console.WriteLine("Error");
    return false;
};

One can log it, throw it, bypass it returning false and even discriminate behaviors having a look at the exception source.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.