C# CsvHelper.ValidationException - Why?

戏子无情 提交于 2020-08-07 05:50:49

问题


I am trying to use CSVHelper in C# console app . I have a this exception:

CsvHelper.ValidationException: 'Header matching ['Numer Dokumentu'] names at index 0 was not found.

And I don't know why becouse this header is on his place in csv file.

Here is my Program.cs

var packs = new List<Pack>();
            using (var streamReader = File.OpenText("C:/.../file.csv"))
            {                
                var reader = new CsvReader(streamReader);
                reader.Configuration.RegisterClassMap<PackMap>();
                packs = reader.GetRecords<Pack>().ToList();
            }

And Pack.cs

public class Pack {   
    public string NrDoc { get; set; }
    public string recipientName { get; set; }
    public string recipientAdress { get; set; }
    public string recipientCity { get; set; }
    public string packValue { get; set; }
    public string packInfo { get; set; }
     }

and PackMap.cs

 sealed class PackMap : ClassMap<Pack>
{
    public PackMap()
    {

        AutoMap();
        Map(m => m.NrDoc).Name("Numer Dokumentu");
        Map(m => m.recipientName).Name("Kontrahent");
        Map(m => m.recipientAdress).Name("Ulica");
        Map(m => m.recipientCity).Name("Miasto");
        Map(m => m.packValue).Name("Brutto");
        Map(m => m.packInfo).Name("Opis");
    }
}

In "PackMap.cs" I tried to use Index(0),Index(1), etc. and no changes. Who will tell me what I doing wrong? I must use different headers name in CSV file and different variables in C#

This is my csv file:

Numer Dokumentu;Status;Data wyst.;Magazyn;Kontrahent;Ulica;Miasto;Netto;Brutto;Opis
FA/3/08/2017/1;;16.08.2017;MAGAZYN;Damianowa Firma;Nowa Lucyna Herc;Lublin;87;20;107;25;Wystawić fakturę. Uwagi klienta:   1
FA/1/10/2017/6;;28.10.2017;MAGAZYN;IBIS Marek Jeż;Jana Pawła II;Szubin;241;00;296;43;Wysyłka
FA/2/10/2017/6;;28.10.2017;MAGAZYN;Netia  S.A.;ul. Poleczki 13;Warszawa;782;28;962;20;Wysyłka pobranie

回答1:


Your file is not comma separated, so you need to change the configuration to use ; instead of a comma.

reader.Configuration.Delimiter = ";";


来源:https://stackoverflow.com/questions/47024481/c-sharp-csvhelper-validationexception-why

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!