6

I have a CSV file that is being exported from another system whereby the column orders and definitions may change. I have found that FileHelpers is perfect for reading csv files, but it seems you cannot use it unless you know the ordering of the columns before compiling the application. I want to know if its at all possible to use FileHelpers in a non-typed way. Currently I am using it to read the file but then everything else I am doing by hand, so I have a class:

[DelimitedRecord(",")]
public class CSVRow
{
    public string Content { get; set; }
}

Which means each row is within Content, which is fine, as I have then split the row etc, however I am now having issues with this method because of commas inherent within the file, so a line might be:

"something",,,,0,,1,,"something else","","",,,"something, else"

My simple split on commas on this string doesnt work as there is a comma in `"something, else" which gets split. Obviously here is where something like FileHelpers comes in real handy, parsing these values and taking the quote marks into consideration. So is it possible to use FileHelpers in this way, without having a known column definition, or at least being able to pass it a csv string and get a list of values back, or is there any good library that does this?

Ryan Amies
  • 4,902
  • 1
  • 21
  • 36

1 Answers1

4

You can use FileHelpers' RunTime records if you know (or can deduce) the order and definitions of the columns at runtime.

Otherwise, there are lots of questions about CSV libraries, eg Reading CSV files in C#

Edit: updated link. Original is archived here

stuartd
  • 70,509
  • 14
  • 132
  • 163
  • I'm not sure the RunTime records would be feasible, as there are >200 columns, but I only care about 2-3 of them, so would I have to type up each column, or can I tell it to ignore the ones I don't need? – Ryan Amies Oct 31 '12 at 16:04
  • 1
    @Stuart's answer is the way to go. Use the CodeBuilder example. Add all 200 columns as `string` columns in a loop and then map the 2 or 3 you need to a simpler class. See my recent answer to a similar question [here](http://stackoverflow.com/a/12895483/1077279) – shamp00 Oct 31 '12 at 16:14
  • 2
    First link is broken :( – Corin May 08 '17 at 03:46
  • Hi, this would be helpful to my scenario, but I cant seem to find the runtime helpers on the link above or the help docs. https://stackoverflow.com/q/53136232/6085193 – Transformer Nov 04 '18 at 03:55
  • I guess things have changed in 6 years. Does https://www.filehelpers.net/docs/html/N_FileHelpers_Dynamic.htm help? – stuartd Nov 04 '18 at 16:50