Using FileHelpers without a type

拈花ヽ惹草 提交于 2019-12-10 13:19:06

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/13161749/using-filehelpers-without-a-type

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!