How to Parser a CSV file in C#

By FoxLearn 3/6/2025 4:17:58 AM   11
Parsing a CSV file in C# can be done in a few different ways depending on your needs.

Below, I will show you both a manual approach and how to use a library (CsvHelper) to parse CSV files more efficiently.

1. Manual Parsing Using StreamReader and String.Split()

This method works well for small or simple CSV files where you don't need advanced features like handling quoted values or errors.

Example CSV File (books.csv):

Title,Author,Year,Genre
1984,George Orwell,1949,Dystopian
Brave New World,Aldous Huxley,1932,Science Fiction
To Kill a Mockingbird,Harper Lee,1960,Fiction

Manual Parsing:

To manually parse this file, you can read the file line by line and split each line by a comma. This gives you a string array containing the fields, which can be processed further. If there’s a header row, skip it (headers are necessary for manual editing and also for third-party parsers).

bool isHeader = true;
foreach (var line in File.ReadLines(@"C:\books.csv"))
{
    if (isHeader)
    {
        isHeader = false;
        continue;
    }

    var fields = line.Split(",");

    // Process the fields
    Console.WriteLine($"Processed book {fields[0]} by {fields[1]} ({fields[2]}) - Genre: {fields[3]}");
}

In this example:

  • The File.ReadLines method reads the file line by line.
  • line.Split(',') splits each line by commas to extract the fields.
  • Skip the header row by using the isHeader flag.

This code parses the CSV file and outputs:

Processed book 1984 by George Orwell (1949) - Genre: Dystopian
Processed book Brave New World by Aldous Huxley (1932) - Genre: Science Fiction
Processed book To Kill a Mockingbird by Harper Lee (1960) - Genre: Fiction

2. Using CsvHelper Library for Easier Parsing

For more complex CSV files, with additional error handling and quoted values, it's better to use a library like CsvHelper.

The CsvHelper library is a powerful and flexible way to parse CSV files in C#. It provides many features such as handling quoted fields, automatic type conversion, and better error handling.

To get started, you need to install the CsvHelper NuGet package.

You can install it via the NuGet Package Manager or using the Package Manager Console:

Install-Package CsvHelper

Example CSV File (books.csv):

Title,Author,Year,Genre,Rating
1984,George Orwell,1949,Dystopian,4.8
Brave New World,Aldous Huxley,1932,Science Fiction,4.5
To Kill a Mockingbird,Harper Lee,1960,Fiction,4.9

Define a Model to Map the CSV Data:

Now, create a Book class that matches the fields in the CSV file:

public class Book
{
    public string Title { get; set; }
    public string Author { get; set; }
    public int Year { get; set; }
    public string Genre { get; set; }
    public double Rating { get; set; }
}

Then, use CsvHelper to parse the CSV file into Book objects:

using CsvHelper;
using System.Globalization;

using (var reader = new StreamReader(@"C:\books.csv"))
using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
{
    foreach (var book in csv.GetRecords<Book>())
    {
        // Process the deserialized records
        Console.WriteLine($"Book: {book.Title} by {book.Author} ({book.Year}) - Genre: {book.Genre} - Rating: {book.Rating}");
    }
}

In this example:

  • StreamReader is used to read the CSV file.
  • CsvReader from the CsvHelper library parses the file.
  • GetRecords<Book>() automatically maps the CSV columns to the Book class properties.
  • The books are processed, and you can perform additional logic like filtering or processing.

Output:

Book: 1984 by George Orwell (1949) - Genre: Dystopian - Rating: 4.8
Book: Brave New World by Aldous Huxley (1932) - Genre: Science Fiction - Rating: 4.5
Book: To Kill a Mockingbird by Harper Lee (1960) - Genre: Fiction - Rating: 4.9

Advantages of Using CsvHelper

  • Automatic Mapping: It automatically maps the CSV header to your class properties.
  • Handles Complex CSVs: Works well with quoted fields, commas inside quotes, and escape characters.
  • Error Handling: Provides detailed error messages for incorrect data types and other issues.
  • Performance: CsvHelper is optimized for performance and can handle large files efficiently.

Loading Records into a Collection

You may want to load all the records into a collection (like a list) for later use.

You can convert the CSV records into a list as shown below:

using CsvHelper;
using System.Globalization;

IEnumerable<T> ParseCsv<T>(string csvFilePath)
{
    using var reader = new StreamReader(csvFilePath);
    using var csv = new CsvReader(reader, CultureInfo.InvariantCulture);

    foreach (var record in csv.GetRecords<T>())
        yield return record;
}

Now, you can use it in your code to load the records:

// To a list
var booksList = ParseCsv<Book>(@"C:\temp\books.csv").ToList();

// Or to a dictionary
var booksByTitleMap = ParseCsv<Book>(@"C:\temp\books.csv").ToDictionary(b => b.Title);

CsvHelper provides detailed error messages, which are very useful for troubleshooting.

For instance, if there’s an invalid value like a letter instead of a number, CsvHelper will give you the exact error message and the row where the error occurred.

Unhandled exception. CsvHelper.TypeConversion.TypeConverterException: The conversion cannot be performed.
    Text: 'a'
    MemberType: System.Double
    TypeConverter: 'CsvHelper.TypeConversion.DoubleConverter'

Performance Comparison: CsvHelper vs Manual Parsing

According to performance tests, CsvHelper performs very well when parsing large files.

Here’s how long it took to parse a CSV file with 1 million records:

  • CsvHelper: 2.4 seconds
  • Manual Parsing (string.Split()): 2.9 seconds
  • TextFieldParser: 18.3 seconds

CsvHelper and manual parsing are about the same speed, with CsvHelper being slightly faster and offering a lot more features and flexibility than manual parsing.