0

I am using CSVHelper libraries of version 2.7.1. I want to read CSV values when the header is present in lowercase. I am reading CSV below

sr = new StreamReader(fs);
using (CsvReader csvReader = new CsvReader(sr))
{
csvReader.Configuration.HasHeaderRecord = hasHeaderRecord;
csvReader.Configuration.IgnoreBlankLines = false;
csvReader.Configuration.IgnoreReadingExceptions = true;
csvReader.Configuration.WillThrowOnMissingField = false;
csvReader.Configuration.TrimFields = true;
csvReader.Configuration.RegisterClassMap();

FileRecords = csvReader.GetRecords().ToList();
}

how to read CSV values with the lower (or) upper case? Can we do without upgrading to the latest CSVHelper packages?

MPelletier
  • 16,256
  • 15
  • 86
  • 137
user768853
  • 269
  • 1
  • 4
  • 16
  • https://learn.microsoft.com/en-us/dotnet/api/system.string.tolower – Hans Passant Jan 30 '23 at 16:37
  • I don't believe the CSVHelper library dictates the case of a CSV header it is reading. Have you got some code that cares about the case of a string? Shouldn't `RegisterClassMap` have a generic type parameter? – Jodrell Jan 30 '23 at 16:38
  • Yeah, make sure the value of hasHeaderRecord is true for this particular file – MPelletier Jan 30 '23 at 16:40
  • 1
    Set `csvReader.Configuration.PrepareHeaderForMatch = PrepareHeaderForMatch = args => args.Header.ToLower()` as shown in [this answer](https://stackoverflow.com/a/67167414/3744182) by Ambrose Leung to [CsvHelper Ignore case for header names](https://stackoverflow.com/q/49521193/3744182). In fact this looks like a duplicate, agree? – dbc Jan 30 '23 at 16:51
  • However, you may want to use `ToLowerInvariant()` instead of `ToLower()` if your CSV file is not localized. – dbc Jan 30 '23 at 16:52
  • @dbc I am using older version of CSVHelper class. So I am not getting PrepareHeaderForMatch – user768853 Jan 31 '23 at 04:42
  • @user768853 - Can you upgrade to a later version? [2.7.1](https://www.nuget.org/packages/CsvHelper/2.7.1) was released on 8/30/2014; the current version, [30.0.1](https://www.nuget.org/packages/CsvHelper/30.0.1), is substantially different and better. – dbc Jan 31 '23 at 05:27

2 Answers2

0

Probably,

var config = new CsvConfiguration(CultureInfo.InvariantCulture)
{
    PrepareHeaderForMatch = args => args.Header.ToLower(),
};
Jodrell
  • 34,946
  • 5
  • 87
  • 124
0

CsvHelper version 2.7.1 had configuration options:

Thus you should be able to set

csvReader.Configuration.IsHeaderCaseSensitive = true;

Do note that, in CsvHelper 3.0, all of these options were replaced with Configuration.PrepareHeaderForMatch:

/// <summary>
/// Prepares the header field for matching against a member name.
/// The header field and the member name are both ran through this function.
/// You should do things like trimming, removing whitespace, removing underscores,
/// and making casing changes to ignore case.
/// </summary>
public virtual Func<string, string> PrepareHeaderForMatch { get; set; } = header => header;

Even if you can't upgrade to the latest version (currently 30.0.1) you might consider at least upgrading to version 3.0. See: CsvHelper Ignore case for header names.

dbc
  • 104,963
  • 20
  • 228
  • 340