ASP.NET Cafe
new tricks every week

CSV to DataTable Parser

Tuesday, 5 February 2008 22:30 by dmitriy

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().

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Categories:   C#
Actions: E-mail | del.icio.us | Kick it! | DZone it! | Permalink | Comments (0) | Comment RSSRSS comment feed

Comments

Add comment


 

  Country flag

biuquote
  • Comment
  • Preview
Loading