Skip to content

CsvReader skipping first (non header) record row when HasHeaderRecord set true. #2204

Answered by AltruCoder
grifja asked this question in Q&A
Discussion options

You must be logged in to vote

The issue is not the ! operator. The issue is your code is telling CsvHelper to skip reading the first row, which has your header. Switching all the true/false responses along with the ! operator will still end up with the same result.

You need to return false to ShouldSkipRecord if it is the header row. You also need to return false if the record is valid. Only return true to ShouldSkipRecord if the record is not valid.

With your original code I get a HeaderValidationException because the code is telling CsvReader to skip the first row if you have a header.

void Main()
{
	var data = @"Id,Name
1,Name #1
2,Name #2";

	var reader = new CsvReader(new StringReader(data), new CsvConfiguration(C…

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@grifja
Comment options

@AltruCoder
Comment options

Answer selected by grifja
@grifja
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants