2014-04-30 15 views
10

Continuo a correre in questo errore:CsvReaderException è stata gestita

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

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

Ma credo che sto seguendo the documentation correttamente. Dopo riferimento alla porzione "Getting Started" Ho implementato questo:

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

e la mia classe:

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; 
} 

mi sento come questo dovrebbe funzionare, perché gli stati di documentazione:

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

Cosa mi manca?

+0

Sto eseguendo lo stesso errore ma ho il {get; impostato;). Hai dovuto fare qualcos'altro per risolvere il problema? –

+1

Il solo fatto di far funzionare una proprietà per me, tuttavia, deve essere una proprietà pubblica. Se tutte le proprietà sono contrassegnate come interne o private, verrà generato lo stesso messaggio di errore. –

+1

In base alla mia esperienza, AutoMapper sembra richiedere che le intestazioni CSV corrispondano ai nomi dei campi, anziché eseguire il mapping per indice. – scotru

risposta

13

La documentazione indica che verrà associata a Properties. La tua classe ha Fields. Apporta questa modifica:

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

Questo cambierà i tuoi campi in "proprietà auto".

+1

Inchiodato. Grazie!!! – drewwyatt

1

è necessario impostare le opzioni di configurazione per la mappatura:

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

Così sembra che devi dirgli di automap. Non ho mai usato questa libreria prima.

Modifica: Jon B l'ha inchiodato.

Problemi correlati