CSVHelper在将区域性从de-DE更改为en-US时引发HeaderValidationException

daolsyd0  于 2023-02-27  发布在  其他
关注(0)|答案(1)|浏览(160)

我正在尝试使用CSVHelper读取CSV文件。它使用以下代码

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();

但是,当我将区域性更改为en-USInvariantCulture时,得到的是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

为什么会出现这种情况呢?头部没有使用任何特殊字符,如ä,ö,ü,ß,而只使用ASCII字符。

pbossiut

pbossiut1#

正如评论中所指出的,CSVHelper在更改文化时似乎使用了不同的分隔符,我将DetectDelimiter = true添加到我的CsvConfiguration中,现在它对两种文化都有效。

相关问题