Is there a default/official/recommended way to parse CSV files in C#? I don\'t want to roll my own parser.
Also, I\'ve seen instances of people using ODBC/OLE DB to
This solution is using the official Microsoft.VisualBasic assembly to parse CSV.
Advantages:
Code:
using Microsoft.VisualBasic.FileIO;
public static List> ParseCSV (string csv)
{
List> result = new List>();
// To use the TextFieldParser a reference to the Microsoft.VisualBasic assembly has to be added to the project.
using (TextFieldParser parser = new TextFieldParser(new StringReader(csv)))
{
parser.CommentTokens = new string[] { "#" };
parser.SetDelimiters(new string[] { ";" });
parser.HasFieldsEnclosedInQuotes = true;
// Skip over header line.
//parser.ReadLine();
while (!parser.EndOfData)
{
var values = new List();
var readFields = parser.ReadFields();
if (readFields != null)
values.AddRange(readFields);
result.Add(values);
}
}
return result;
}