Possible Duplicate:
CSV File Imports in .Net
In .net, is there a standard library that should be used to read in csv files? All the samples on the web roll their own csv reader / parser, or use OleDb.
It's not a problem using any of these solutions, I was just wondering if there is a generally accepted library (not that I can find), or any other "proper" way to do it?
One of the reasons that many people write their own is that CSV isn't quite so simple. For example:
These are some of the reasons people write their own parsers, because they're stuck reading files created with all these different settings. Or they write their own serializers, because the target system has a bunch of these idiosyncrasies.
If you don't care about these issues, just use the most convenient library. But understand they are there.
After some more investigation, there is also this: http://www.filehelpers.com/
It seems a full framework around reading files, and not just csv files.
(note: just read stuff on the website, have not used it yet)
Try CsvHelper (a library I maintain). It's also available via NuGet.
CsvHelper allows you to read your CSV file directly into your custom class.
CsvReader will automatically figure out how to match the property names based on the header row (this is configurable). It uses compiled expression trees instead of reflection, so it's very fast.
It is also very extensible and configurable.
I'm pretty sure you can read a CSV file into a DataTable with one line of code. Once it's in a DataTable, you can sort, filter, iterate etc.
This question has some examples for reading CSVs into DataTables.
CsvReader is a pretty good one... it isn't Microsoft, but it works very well, and is a lot faster than some of the alternatives (legacy OleDb etc).
The VB namespace has a great TextFieldParser class. I know, c# people don't like to reference a library from that 'basic' language, but it is quite good.
It is located at Microsoft.VisualBasic.FileIO.TextFieldParser
I used to mess with OLEDB, creating column definition files etc - but find the TextFieldParser a very simple and handy tool for parsing any delimited files.