0

I'm having trouble debugging a ServiceStack.Text string FromCsv call. I am parsing several csv documents but one document keeps throwing an exception. I can't determine the root cause. The exception is:

"Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index"

I understand the logic of the exception. It's trying to access the collection using an out of range index. I just can't seem to find the root cause in the underlying data.

It is just one document that throws this exception. Many others parse without exception.

Here is my code:

string columnHeaderRow = "ItemNumber`ItemName`Description\n\r";
string rawData = "my UTF8 encoded delimited data";
CsvConfig.ItemSeperatorString = "`";
string dataToParse = columnHeaderRow + rawData;
var data = dataToParse.FromCsv<Models.Ticket[]>(); // Exception thrown here

This is in an asp.net core app and I am referencing ServiceStack.Text.Core v1.0.37

Any thoughts will be much appreciated!

brady321
  • 1,475
  • 13
  • 14
  • 1
    Might be unrelated, but a Windows line separator is `\r\n`, not `\n\r` like you have on your first line. I'd try reducing the size of the document until you find the problem - this process is usually the quickest. – Kobi Mar 06 '17 at 19:26
  • Both line separators worked. Your suggestion to break the document down helped me find the problem. I ended up having to call FromCsv for each row individually to catch the row causing the exception. (One row had an extra delimiter) This approach essentially broke the document into many one row documents. Maybe there is a better approach with ServiceStack.Text but I wasn't able to find one. This parse one line at a time approach allowed me to catch and handle the bad row without throwing out the whole document. Thanks for the suggestions they led me down the right path. – brady321 Mar 08 '17 at 21:42

0 Answers0