2014-04-30 18 views
10

Ciągle działa w ten błąd:CsvReaderException był nieobsługiwany

An unhandled exception of type 'CsvHelper.CsvReaderException' occurred in CsvHelper.dll

Additional information: No properties are mapped for type 'RPS_String_Parse.Program+FormattedRow'.

Ale wierzę śledzę the documentation poprawnie. Po przedstawieniu w "Getting Started" część I wdrożone następująco:

using (var sr = new StreamReader(filePath)) 
{ 
    var csv = new CsvReader(sr); 
    var records = csv.GetRecords<FormattedRow>(); 
    foreach (var record in records) 
    { 
     Console.WriteLine(record.Address1); 
    } 

    Console.ReadLine(); 
} 

i moja klasa:

public class FormattedRow 
{ 
     public string IDOrderAlpha; 
     public string IDOrder; 
     public string AddressCompany; 
     public string Address1; 
     public string Address2; 
     public string AddressCity; 
     public string AddressState; 
     public string AddressZip; 
     public string AddressCountry; 
     public string ShipMethod; 
     public string ContactEmail; 
     public string ContactName; 
     public string ServiceRep; 
     public string CustomerPuchaseOrder; 
} 

czuję się jak to powinno działać, ponieważ stanach dokumentacja:

Auto Mapping

If you don't supply a mapping file, auto mapping will be used. Auto mapping will map the properties in your class in the order they appear in. If the property is a custom class, it recursively maps the properties from that class in the order they appear in. If the auto mapper hits a circular reference, it will stop going down that reference branch

czego mi brakuje?

+0

Używam tego samego błędu, ale mam {get; zestaw;). Czy musisz zrobić cokolwiek innego, aby rozwiązać problem? –

+1

Po prostu sprawienie, by działka była dla mnie dobra, ale musi to być własność publiczna. Jeśli wszystkie właściwości są oznaczone jako wewnętrzne lub prywatne, spowoduje to ten sam komunikat o błędzie. –

+1

Z mojego doświadczenia wynika, że ​​AutoMapper wydaje się wymagać, aby nagłówki CSV pasowały do ​​nazw pól zamiast mapowania według indeksu. – scotru

Odpowiedz

13

Dokumentacja stwierdza, że ​​będzie mapować na Properties. Twoja klasa ma Fields. Wprowadź tę zmianę:

public class FormattedRow 
{ 
    public string IDOrderAlpha { get; set; } 
    // add { get; set; } for all 
} 

Spowoduje to zmianę pól na "właściwości automatyczne".

+1

Przybity to. dzięki!!! – drewwyatt

1

musisz ustawić opcje konfiguracyjne dla mapowania: pojawia Więc

var generatedMap = csv.Configuration.AutoMap<MyClass>(); 

to trzeba powiedzieć go do Automapa. Nigdy wcześniej nie korzystałem z tej biblioteki.

Edytuj: Jon B przybił to.