2

I'm trying to read a CSV file sent from an upload form and convert it to my Poco. Here is my test file:

"30247685204","PWITA1","114000/2017/SE","","27/11/2017","1027/00","","","ZZPWI1","INTER MEMBER1",
"30247685205","PWITA2","114000/2017/SE","","27/11/2017","1027/00","","","ZZPWI2","INTER MEMBER2"

Here is my Poco:

public class MyOrder
{
    public string TrackingID { get; set; }
    public string CarrierName { get; set; }
    public string ConNo { get; set; }
    public string import_id { get; set; }
    public string EnteredDate { get; set; }
    public string CustomerRef { get; set; }
    public string ConDescription { get; set; }
    public string TransferAcCode { get; set; }
    public string AccountNo { get; set; }
    public string AccountName { get; set; }
}

Here is how I get the file content in my service:

foreach (var uf in Request.Files.Where(f => f.ContentLength > 0))
{
    var content = new StreamReader(uf.InputStream).ReadToEnd();
    var orders = content.FromCsv<List<MyOrder>>();
}

If I break on var content I can see that I get the file content as so:

"\"30247685204\",\"PWITA1\",\"114000/2017/SE\",\"\",\"27/11/2017\",\"1027/00\",\"\",\"\",\"ZZPWI1\",\"INTER MEMBER1\",\r\n\"30247685205\",\"PWITA2\",\"114000/2017/SE\",\"\",\"27/11/2017\",\"1027/00\",\"\",\"\",\"ZZPWI2\",\"INTER MEMBER2\"\r\n"

The problem is that I can't get the correct list of my Poco orders in var orders. I get just one element (in the file I have two) and it's empty... all the properties are set to null! I always handle JSON object and I had no problem in the past, this is my first attempt to read a CSV file and I can't understand where I'm wrong!

pardie
  • 399
  • 2
  • 16

1 Answers1

2

You can specify not to treat the first row of the CSV as headers with:

CsvConfig<MyOrder>.OmitHeaders = true;

Preferably static config should be set once in your AppHost Configure(), then you can deserialize it with:

var orders = CsvSerializer.DeserializeFromStream<List<MyOrder>>(uf.InputStream);
mythz
  • 141,670
  • 29
  • 246
  • 390