Using FileHelpers without a type
Asked Answered
S

1

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?

Swan answered 31/10, 2012 at 15:46 Comment(0)
A
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

Amparoampelopsis answered 31/10, 2012 at 15:55 Comment(5)
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?Swan
@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 hereAnglophobia
First link is broken :(Maddi
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. stackoverflow.com/q/53136232/6085193Shewchuk
I guess things have changed in 6 years. Does filehelpers.net/docs/html/N_FileHelpers_Dynamic.htm help?Amparoampelopsis

© 2022 - 2024 — McMap. All rights reserved.