CsvGatling: a C# CSV parser


CsvGatling


CsvGatling is a fast, scalable CSV parser with a simple C# interface.  The source code is hosted on GitHub, and I licensed it public domain.

Minimal Example


Imagine we have this CSV file:

Name,FavoriteColor,BirthDate
John,Red,11/2/1980
Ted,Green,4/3/1989
Chris,Blue,11/10/2000

We’ll create a class to map each row to:

public class Person
{
    public DateTime BirthDate { get; set; }
    public string Name { get; set; }
    public string FavoriteColor { get; set; }
}

Then, to read the file:

using System;
using System.IO;
using CsvGatling;

using (FileStream fileStream = new FileStream(@"C:\Path\To\CSV\File", FileMode.Open))
{
    ICsvReader csvReader = new CsvReader(fileStream);
    foreach (Person person in csvReader.ReadAll())
    {
        Console.WriteLine("{0} was born on {1:M/d/yyyy}, and his favorite color is {2}",
            person.Name,
            person.BirthDate,
            person.FavoriteColor);

    }
 }

The output will be:

John was born on 11/2/1980, and his favorite color is Red
Ted was born on 4/3/1989, and his favorite color is Green
Chris was born on 11/10/1900, and his favorite color is Blue

Design


CsvGatling…

…is simple. As shown in the example above, its very simple interface makes it extremely easy to use.

…is streaming. It will not try to read the entire file into memory; it will only read rows into memory as they are being read.

…is scalable. There are no size limits, and the library can easily read CSV files of any number of rows and any number of columns.

…is fast. It uses System.Reflection to map column names to properties with the same name, and usesSystem.Reflection.Emit to dynamically generate code to avoid the need to use Reflection for each row.

…follows the RFC 4180 standard, with a few caveats. By default, fields are comma separated, lines are new line separated, quotes use double-quotes, and quotes are escaped by having two neighboring quotes, just as the standard says. (The caveats are: (1) CsvGatling will work for both UNIX (\n) and Windows (\r\n) line endings. (2) Files must have a header. (3) Users can choose to use different column delimiter and quote characters with the appropriate method overloads.)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s