6 May 2012

The simplest way to parse csv files. I am using external library that does the magic for you. Csv file should be separated by commas and strings with double quotes.

Download "A Fast CSV Reader":At first you have to import the external library. It's called A Fast CSV Reader, made by Sebastien Lorion. Download binaries from codeproject.com. It's shown there as well how to use it. I am going to show my example, maybe its more straight forward.
Add "A Fast CSV Reader" to your project:Your project, there are References. You can see them from Solution Explorer. Just add reference and from Browse tab select you downloaded dll file. Also add it with the using statement.
Source code viewer
  1. using LumenWorks.Framework.IO.Csv;
Programming Language: C#
Using "A Fast CSV Reader":
Source code viewer
  1. List<string> product_codes = new List<string>();
  2. using(CsvReader csv = new CsvReader(new StreamReader(input_file_dir.Text), true))
  3. {
  4. int fieldCount = csv.FieldCount;
  5.  
  6. string[] headers = csv.GetFieldHeaders();
  7. while (csv.ReadNextRecord())
  8. {
  9. for (int i = 0; i < fieldCount; i++)
  10. {
  11. product_codes.Add(csv[i]);
  12. }
  13. }
  14. }
Programming Language: C#
Now you have a list that includes your csv file.