CodeOfChaos.Parsers.Csv 2.5.0

Prefix Reserved
dotnet add package CodeOfChaos.Parsers.Csv --version 2.5.0
                    
NuGet\Install-Package CodeOfChaos.Parsers.Csv -Version 2.5.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="CodeOfChaos.Parsers.Csv" Version="2.5.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CodeOfChaos.Parsers.Csv" Version="2.5.0" />
                    
Directory.Packages.props
<PackageReference Include="CodeOfChaos.Parsers.Csv" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add CodeOfChaos.Parsers.Csv --version 2.5.0
                    
#r "nuget: CodeOfChaos.Parsers.Csv, 2.5.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package CodeOfChaos.Parsers.Csv@2.5.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=CodeOfChaos.Parsers.Csv&version=2.5.0
                    
Install as a Cake Addin
#tool nuget:?package=CodeOfChaos.Parsers.Csv&version=2.5.0
                    
Install as a Cake Tool

CodeOfChaos.Parsers.Csv

CodeOfChaos.Parsers.Csv is a lightweight library for parsing CSV files in .NET with an API inspired by conventional XML parsing. It supports handling CSV data in multiple formats such as objects, dictionaries, and enumerable collections, with both synchronous and asynchronous processing capabilities.


Features

CSV Parsing

Transform CSV content into multiple output formats:

  • Object Mapping – Convert CSV rows directly into objects.
  • Dynamic Parsing – Parse CSV data into dictionaries for flexible usage.
  • Enumerable Support – Process rows in batches or streams to handle large files efficiently.
  • Header Mapping – Automatically map CSV headers to object properties using configurable behavior.

Writing CSV

Create CSV files or strings from .NET objects:

  • Write objects directly to CSV, with easy-to-use serialization.
  • Support for both synchronous and asynchronous writing.

Configuration Options

Customize CSV parsing and writing:

  • Delimiters – Support different separators like ,, ;, or custom characters.
  • Header Handling – Include or exclude headers.
  • Error Logging – Log and handle errors gracefully.
  • Batch Processing – Manage large files by processing data in chunks.

Attribute-Based Column Mapping

Map CSV columns explicitly to object properties using attributes:

  • [CsvColumn("ColumnName")] maps specific CSV headers to class properties.
  • Useful for handling CSVs with non-standard or verbose field names.

Installation

This library targets .NET 9.0 and requires C# 13.0. Ensure your project meets these requirements before using.

Add the dependency to your project via NuGet:

dotnet add package CodeOfChaos.Parsers.Csv --version 2.0.0-preview.0

Usage

Here’s how you can leverage the CodeOfChaos.Parsers.Csv library:

Configuring the Parser

You can create and configure a CsvParser instance using the CsvParser.FromConfig method. For example:

var parser = CsvParser.FromConfig(cfg => {
    cfg.ColumnSplit = ";";        // Use `;` as the delimiter
    cfg.IncludeHeader = true;     // Parse CSV with a header row
    cfg.BatchSize = 100;          // Process data in smaller batches
});

Parsing CSV to Objects

using CodeOfChaos.Parsers.Csv;

class Person {
    public string Name { get; set; } = default!;
    public int Age { get; set; }
}

var parser = CsvParser.FromConfig(cfg => cfg.ColumnSplit = ",");
var people = parser.ToList<Person>("path/to/file.csv");

foreach (var person in people) {
    Console.WriteLine($"{person.Name} is {person.Age} years old.");
}

Parsing CSV to Dictionary

If you need more dynamic handling and don't want to use object mapping, you can parse CSV data into a dictionary:

using CodeOfChaos.Parsers.Csv;

var parser = CsvParser.FromConfig(cfg => cfg.ColumnSplit = ",");
IEnumerable<Dictionary<string, string?>> rows = parser.ToDictionaryEnumerable("path/to/file.csv");

foreach (var row in rows) {
    Console.WriteLine($"Name: {row["Name"]}, Age: {row["Age"]}");
}

Handling Large Files with Enumeration

For large files, you can use ToEnumerable for lazy loading and processing of rows:

var parser = CsvParser.FromConfig(cfg => cfg.ColumnSplit = ",");
await foreach (var person in parser.ToEnumerableAsync<Person>("path/to/large-file.csv")) {
    Console.WriteLine($"{person.Name} is {person.Age} years old.");
}

Writing Objects to CSV

Write a collection of objects back to a CSV file:

using CodeOfChaos.Parsers.Csv;

var people = new[] {
    new Person { Name = "John", Age = 30 },
    new Person { Name = "Jane", Age = 25 }
};

var parser = CsvParser.FromConfig(cfg => cfg.ColumnSplit = ",");
parser.ParseToFile("output.csv", people);

Using Attributes for Column Mapping

Use the [CsvColumn] attribute to map CSV columns to object properties:

using CodeOfChaos.Parsers.Csv;

class User {
    [CsvColumn("username")]
    public string Name { get; set; } = default!;

    [CsvColumn("age")]
    public int Age { get; set; }
}

var parser = CsvParser.FromConfig(cfg => cfg.ColumnSplit = ",");
var users = parser.ToList<User>("path/to/file.csv");

// Input CSV example:
// username,age
// John,30
// Jane,25

foreach (var user in users) {
    Console.WriteLine($"{user.Name} is {user.Age} years old.");
}

Contributing

Feel free to fork and contribute to the project by submitting pull requests. When contributing, ensure your changes align with the project’s coding standards.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.5.0 113 1/29/2025
2.4.0 135 1/1/2025
2.3.0 122 12/31/2024
2.2.0 90 12/22/2024
2.1.3 87 12/21/2024
2.0.0 119 12/10/2024
1.1.0 125 12/9/2024
1.0.0 119 12/9/2024