英文:
CSVHelper throws HeaderValidationException when changing the culture from de-DE to en-US
问题
I'm trying to read a CSV file using CSVHelper.
It is working with the following code
record Row(string anbieter, string produkt, string? ean, string projektnr, string produktkennzeichen, string produktkategorie, string produktlink);
var config = new CsvConfiguration(CultureInfo.GetCultureInfo("de-DE"))
{
HasHeaderRecord = true
};
using var file = File.OpenText("file.csv");
using var reader = new CsvReader(file, config);
var rows = reader.GetRecords<Row>().ToList();
However when I change the culture to en-US
or InvariantCulture
, I'm getting a HeaderValidationException
.
Header with name 'anbieter'[0] was not found.
Header with name 'produkt'[0] was not found.
Header with name 'ean'[0] was not found.
Header with name 'projektnr'[0] was not found.
Header with name 'produktkennzeichen'[0] was not found.
Header with name 'produktkategorie'[0] was not found.
Header with name 'produktlink'[0] was not found.
Header with name 'anbieter'[0] was not found.
Header with name 'produkt'[0] was not found.
Header with name 'ean'[0] was not found.
Header with name 'projektnr'[0] was not found.
Header with name 'produktkennzeichen'[0] was not found.
Header with name 'produktkategorie'[0] was not found.
Header with name 'produktlink'[0] was not found.
Headers: 'anbieter;produkt;ean;projektnr;produktkennzeichen;produktkategorie;produktlink'
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.
IReader state:
ColumnCount: 0
CurrentIndex: -1
HeaderRecord:
["anbieter;produkt;ean;projektnr;produktkennzeichen;produktkategorie;produktlink"]
IParser state:
ByteCount: 0
CharCount: 80
Row: 1
RawRow: 1
Count: 1
RawRecord:
anbieter;produkt;ean;projektnr;produktkennzeichen;produktkategorie;produktlink
Why does that happen? The header is not using any special characters like ä, ö, ü, ß, but only ASCII characters.
英文:
I'm trying to read a CSV file using CSVHelper.
It is working with the following code
record Row(string anbieter, string produkt, string? ean, string projektnr, string produktkennzeichen, string produktkategorie, string produktlink);
var config = new CsvConfiguration(CultureInfo.GetCultureInfo("de-DE"))
{
HasHeaderRecord = true
};
using var file = File.OpenText("file.csv");
using var reader = new CsvReader(file, config);
var rows = reader.GetRecords<Row>().ToList();
However when I change the culture to en-US
or InvariantCulture
, I'm getting a HeaderValidationException
.
Header with name 'anbieter'[0] was not found.
Header with name 'produkt'[0] was not found.
Header with name 'ean'[0] was not found.
Header with name 'projektnr'[0] was not found.
Header with name 'produktkennzeichen'[0] was not found.
Header with name 'produktkategorie'[0] was not found.
Header with name 'produktlink'[0] was not found.
Header with name 'anbieter'[0] was not found.
Header with name 'produkt'[0] was not found.
Header with name 'ean'[0] was not found.
Header with name 'projektnr'[0] was not found.
Header with name 'produktkennzeichen'[0] was not found.
Header with name 'produktkategorie'[0] was not found.
Header with name 'produktlink'[0] was not found.
Headers: 'anbieter;produkt;ean;projektnr;produktkennzeichen;produktkategorie;produktlink'
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.
IReader state:
ColumnCount: 0
CurrentIndex: -1
HeaderRecord:
["anbieter;produkt;ean;projektnr;produktkennzeichen;produktkategorie;produktlink"]
IParser state:
ByteCount: 0
CharCount: 80
Row: 1
RawRow: 1
Count: 1
RawRecord:
anbieter;produkt;ean;projektnr;produktkennzeichen;produktkategorie;produktlink
Why does that happen? The header is not using any special characters like ä, ö, ü, ß, but only ASCII characters.
答案1
得分: 1
CSVHelper 似乎在更改文化时使用了不同的分隔符,根据评论中指出的情况。
我在我的 CsvConfiguration
中添加了 DetectDelimiter = true
,现在它可以在两种文化下工作了。
英文:
As pointed out in the comments, CSVHelper seems to use a different delimiter when changing the culture.
I added DetectDelimiter = true
to my CsvConfiguration
and it is now working for both cultures.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论