Recently I need to parse CSV (comma separated values) text data into datatable. Standard format where first line is column names, values are separated by commas and possible quoted.
I've used powerful regex in .net to parse that data:
"(\"([^\"]*|\"{2})*\"(,|$))|\"[^\"]*\"(,|$)|[^,]+(,|$)|(,)"
As usually it's really unreadable. :)
And as result I've a class to parse CSV to datatable. Not much comments, just want to post the class, because was really sad after search in Google... there are no ready solution for this common problem (if you know some - let me know in comments).
CSVParser.cs (2.41 kb)
Easy to use - just create object and pass your stream with CSV data as parameter. And call ParseToDataTable().