2014-04-30 13 views
10

tôi tiếp tục chạy vào lỗi này:CsvReaderException là unhandled

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

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

Nhưng tôi tin rằng tôi đang theo the documentation một cách chính xác. Sau khi tham khảo phần "bắt đầu" tôi thực hiện điều này:

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

và lớp học của tôi:

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

tôi cảm thấy như thế này nên làm việc, bởi vì các trạng thái tài liệu:

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

Tôi đang thiếu gì?

+0

Tôi đang chạy đến cùng một lỗi nhưng tôi có {get; bộ;). Bạn có phải làm gì khác để khắc phục sự cố không? –

+1

Chỉ cần biến nó thành tài sản đã làm việc cho tôi, tuy nhiên nó phải là tài sản công cộng. Nếu tất cả các thuộc tính được đánh dấu là nội bộ hoặc riêng tư, nó sẽ dẫn đến thông báo lỗi tương tự. –

+1

Theo kinh nghiệm của tôi, AutoMapper dường như yêu cầu tiêu đề CSV để khớp với tên trường, thay vì lập bản đồ theo chỉ mục. – scotru

Trả lời

13

Tài liệu tuyên bố rằng nó sẽ ánh xạ tới Properties. Lớp học của bạn có Fields. Thực hiện thay đổi này:

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

này sẽ thay đổi lĩnh vực của bạn để "tính tự động".

+1

Đã đóng đinh nó. cảm ơn!!! – drewwyatt

1

Bạn cần phải thiết lập các tùy chọn cấu hình để lập bản đồ:

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

Vì vậy, nó xuất hiện bạn cần phải nói cho nó để AutoMap. Tôi chưa bao giờ sử dụng thư viện này trước đây.

Chỉnh sửa: Jon B đóng đinh nó.

Các vấn đề liên quan