Csvhelper: Get Class Records throws on sample from documentation

Created on 4 Jul 2019  路  2Comments  路  Source: JoshClose/CsvHelper

Describe the bug
Reading records using csv.GetRecords<Type>() and I get CsvHelper.HeaderValidationException

To Reproduce
Steps to reproduce the behavior:

  1. use https://joshclose.github.io/CsvHelper/examples/reading/get-class-records
  2. create a net462 or netcore21 console app
  3. add enumerating of records
  4. run code
  5. get error:
Unhandled Exception: CsvHelper.HeaderValidationException: Header with name 'Id' was not found. If you are expecting some headers to be missing and want to ignore this validation, set the configuration HeaderValidated to null. You can also change the functionality to do something else, like logging the issue.
   at CsvHelper.Configuration.ConfigurationFunctions.HeaderValidated(Boolean isValid, String[] headerNames, Int32 headerNameIndex, ReadingContext context)
   at CsvHelper.CsvReader.ValidateHeader(ClassMap map)
   at CsvHelper.CsvReader.<GetRecords>d__63`1.MoveNext()
   at csvtest.Program.Main()

Expected behavior
Records are parsed into class objects.

Screenshots
If applicable, add screenshots to help explain your problem.

Additional context
my code:
file.csv

Id,Name
1,one

```C#
using System.IO;
using CsvHelper;

namespace csvtest
{
class Program
{
public static void Main()
{
using (var reader = new StreamReader("file.csv"))
using (var csv = new CsvReader(reader))
{
var records = csv.GetRecords();
foreach(var r in records)
{
System.Console.WriteLine(r);
}
}
}
}

public class Foo
{
    public int Id { get; set; }
    public string Name { get; set; }
}

}
```

bug

Most helpful comment

Try manually setting the delimiter, sometimes your OS settings for CSV's overwrites the default comma delimiter.
csv.Configuration.Delimiter = ",";

All 2 comments

Try manually setting the delimiter, sometimes your OS settings for CSV's overwrites the default comma delimiter.
csv.Configuration.Delimiter = ",";

Try manually setting the delimiter, sometimes your OS settings for CSV's overwrites the default comma delimiter.
csv.Configuration.Delimiter = ",";

Thank you, this solved the issue for me.
Added a PR with updated docs: https://github.com/JoshClose/CsvHelper/pull/1349

Was this page helpful?
0 / 5 - 0 ratings

Related issues

CallMeBruce picture CallMeBruce  路  4Comments

JoshClose picture JoshClose  路  5Comments

rh78 picture rh78  路  3Comments

RizwanAhmedJutt picture RizwanAhmedJutt  路  5Comments

DmitryEfimenko picture DmitryEfimenko  路  3Comments