Spaces before column names in header row cause HeaderValidationException #1496
Unanswered
rickhodder
asked this question in
Q&A
Replies: 1 comment
-
Why are there spaces in the header fields and only after the first field? Is this just because you did a hand written example or are you getting files like this? You can either specify the name with a space in it. public class MyRow
{
public string Foo {get;set;}
[Name(" Bar")]
public string Bar {get;set;}
[Name(" Example")]
public string Example {get;set;}
} Or you can trim the spaces from the header when matching against your property names. csv.Configuration.PrepareHeaderForMatch = (header, index) => header.Trim(); |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
When there are spaces before the beginning of column names in the header row, a HeaderValidationException is thrown, saying that the columns dont exist.
To Reproduce
public class MyRow
{
public string Foo {get;set;}
public string Bar {get;set;}
public string Example {get;set;}
}
File contents: headerdemo.txt:
Foo, Bar, Example
F,B,E
Code Sample
using (var reader = new StreamReader(@".\headerdemo.txt"))
using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
{
var records = csv.GetRecords().ToList();
return records;
}
Foo,Bar, Example
Foo,Bar,Example
Expected behavior
To ignore initial spaces after commas in header row
Beta Was this translation helpful? Give feedback.
All reactions