Reading CSV files using CSVHelper package (C#, IEnumerable)

I have come across many times the need to incorporate the reading of CSV files in an application.  Below I will show how to use Josh Close’s CsvHelper to  import a CSV file which looks like the following (file will be included below).

CommonName,FormalName,TelephoneCode,CountryCode
Argentina,Argentine Republic,54,ARG
Armenia,Republic of Armenia,374,ARM
Australia,Commonwealth of Australia,61,AUS
Austria,Republic of Austria,43,AUT
“Bahamas, The”,Commonwealth of The Bahamas,-241,BHS
Bangladesh,People’s Republic of Bangladesh,880,BGD

Note this CSV file is a simpler file based on the file which is located here.

The first thing to do is to Install CsvHelper.  To do this run the following command in the Package Manager Console:

Now add CsvHelper to the program by adding:

The next step is to create a class which has properties with the same name of the column headings found in the csv file.  Below you will find an example of a class which does this:

Finally create an instance of CSVReader and invoke the GetRecords method using the DataRecord class.  Below you will find an example of this:

An completed example can be found below:

You can download this sample code with  CsvHelper and CSV file here: CVSHelperReadSample

Summary
Article Name
Reading CSV files using CSVHelper package (C#, IEnumerable)
Author
Description
Sample code on of how to read a CSV file with C# and the CSVHelper package (IEnumerable).

Creative Commons License
Reading CSV files using CSVHelper package (C#, IEnumerable) by Carlos Ferreira, unless otherwise expressly stated, is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

About Carlos Ferreira

Comments

  1. Jason Canter says:

    Thanks!

  2. Luis Santos says:

    You have a small type before creation of the reader class. Thanks for putting this up.

  3. Csvreader doesnt have a GetRecords

    • Hi Matt –

      Sounds like you might have not loaded CSVHelper correctly in your project. Follow the steps for adding it via Nuget in your project. There’s a sample project you can download if you want to experiment (link is at bottom of screen).

      Cheers,

      Carlos

  4. Ah, I figured it out. I had my .Net framework set to 2.0 when I installed the csvhelper package. Uninstall, change to 4.0, reinstall fixed it.

    Also, I noticed there is a difference between this page and the sample project.

    IEnumerable should be IEnumerable

  5. We are trying to parse a csv file in which the the field delimiters are identified by ^ and end of a record identified by ~. I know there is an option for setting the field delimiter in csvhelper but how do we tell it the delimiter for end of a record.
    Many Thanks

    • Hi Hassan –

      Thanks for visiting. I looked at the source and it looks like there is not. It doesn’t look like it would be difficult to change if you look at the ReadLine method in CsvParser.cs. I would think that this would be useful for parsing files from legacy systems. i might have a stab at seeing if it can be added later on.

      Cheers!

      Carlos

      • That would be great. Thanks.
        I just thought of something else, the file we are trying to parse has text fields which could have carriage returns in them so making the end of a record something other than newline is mandatory in situations like this.
        Thanks again.
        Hassan

Trackbacks

  1. […] is a follow-up on my post on how to read a CSV file using Josh Close’s CsvHelper.  CsvHelper is a fast and […]

  2. Stefan says:

    Stefan

    Reading CSV files using CSVHelper package (C#, IEnumerable)

Leave a Reply

%d bloggers like this: