Is there a "proper" way to read CSV files [duplicate]
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?
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).
One of the reasons that many people write their own is that CSV isn't quite so simple. For example:
- Does the first row contain field names, or not?
- Do you support dates? If, so, are they quoted, surrounded by # marks, in a certain day-month-year order?
- Does it support linefeeds that occur inside quoted text values? Or does that split the record?
- How do you escape a quote inside of a quoted string? Do you double the quote, or use a backslash or other escape character?
- What character encoding(s) are supported?
- How does it handle escaped control characters? &#XX; or \uXXXX or some other method?
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.
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.