Meziantou.Framework.Csv 6.0.2

Prefix Reserved
dotnet add package Meziantou.Framework.Csv --version 6.0.2
                    
NuGet\Install-Package Meziantou.Framework.Csv -Version 6.0.2
                    
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="Meziantou.Framework.Csv" Version="6.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Meziantou.Framework.Csv" Version="6.0.2" />
                    
Directory.Packages.props
<PackageReference Include="Meziantou.Framework.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 Meziantou.Framework.Csv --version 6.0.2
                    
#r "nuget: Meziantou.Framework.Csv, 6.0.2"
                    
#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 Meziantou.Framework.Csv@6.0.2
                    
#: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=Meziantou.Framework.Csv&version=6.0.2
                    
Install as a Cake Addin
#tool nuget:?package=Meziantou.Framework.Csv&version=6.0.2
                    
Install as a Cake Tool

Meziantou.Framework.Csv

A high-performance .NET library to read and write CSV data, and to map CSV records to .NET types.

Features

  • SIMD parsing - The separators, quotes and line terminators are found 16, 32 or 64 characters at a time (AVX-512, AVX2, SSE, NEON), and the values are slices of the buffer: reading a record does not allocate
  • UTF-16 and UTF-8 - CsvReader and CsvWriter work on text, Utf8CsvReader and Utf8CsvWriter on UTF-8 bytes without decoding them
  • Any source - Strings, memory, TextReader, Stream, IBufferWriter<T> and files, synchronously or asynchronously
  • Serializer - Map records to classes, structs and records with CsvSerializer, attributes, naming policies and converters, in the style of System.Text.Json
  • Native AOT - A source generator writes the serialization code at build time
  • Real-world files - Custom separators and quotes, separator detection, comments, empty lines, trimming, strict mode, CSV injection protection

Usage

The types are in the Meziantou.Framework.Csv and Meziantou.Framework.Csv.Serialization namespaces.

Reading CSV data

using var reader = CsvReader.CreateFromString("""
    Name,Age
    Alice,30
    Bob,25
    """);

var lines = new List<string>();
while (reader.Read())
{
    var record = reader.Current;
    lines.Add($"{record["Name"].Span} is {record["Age"].Parse<int>()} years old");
}

Assert.Equal(["Alice is 30 years old", "Bob is 25 years old"], lines);

Reading UTF-8 data

Prefer Utf8CsvReader for UTF-8 files, streams and bytes: it has the same options and members as CsvReader, and does not decode the values you do not read.

using var stream = new MemoryStream("Name,Age\nAlice,30\nBob,25"u8.ToArray());
using var reader = Utf8CsvReader.Create(stream);

var totalAge = 0;
var names = new List<string>();
foreach (var record in reader)
{
    names.Add(record["Name"].ToString());
    totalAge += record["Age"].Parse<int>(); // Parses the UTF-8 bytes, without decoding them
}

Assert.Equal(["Alice", "Bob"], names);
Assert.Equal(55, totalAge);

Writing CSV data

var output = new StringBuilder();
using (var writer = CsvWriter.Create(output, new CsvWriterOptions { NewLine = "\n" }))
{
    writer.WriteRow("Name", "Age", "City");
    writer.WriteRow("Alice", "30", "New York");

    // Write a row value by value: numbers are formatted in the buffer, with the invariant culture
    writer.WriteField("Bob");
    writer.WriteField(25);
    writer.WriteField("London, UK"); // Quoted: the value contains the separator
    writer.EndRow();
} // Disposing the writer flushes the data

Assert.Equal("Name,Age,City\nAlice,30,New York\nBob,25,\"London, UK\"\n", output.ToString());

Prefer Utf8CsvWriter to write UTF-8 files, streams and IBufferWriter<byte>: it formats values directly to UTF-8 bytes.

Serializing objects

public sealed class Person
{
    public string? Name { get; set; }

    public int Age { get; set; }

    public DateOnly? BirthDate { get; set; }
}
var people = new List<Person>
{
    new() { Name = "Alice", Age = 30, BirthDate = new DateOnly(1995, 4, 12) },
    new() { Name = "Bob, Jr.", Age = 25 },
};

var csv = CsvSerializer.Serialize(people);
Assert.Equal("Name,Age,BirthDate\r\nAlice,30,1995-04-12\r\n\"Bob, Jr.\",25,\r\n", csv);

var result = CsvSerializer.Deserialize<Person>(csv);
Assert.Equal("Bob, Jr.", result[1].Name);
Assert.Null(result[1].BirthDate); // An empty value is null for a nullable type

CsvSerializer.SerializeToUtf8Bytes returns the UTF-8 data as a byte[], without creating a string first.

Source generation for Native AOT

[CsvSerializable(typeof(Product))]
[CsvSerializable(typeof(Person))]
[CsvSourceGenerationOptions(Separator = ';')]
public partial class AppCsvContext : CsvSerializerContext
{
}
Product[] products = [new(1, "Keyboard", 49.90m), new(2, "Mouse", 19.99m)];

// The metadata comes from the generated code: no reflection, compatible with trimming and Native AOT
var csv = CsvSerializer.Serialize(products, AppCsvContext.Default.Product);
Assert.Equal("Id;Name;Price\r\n1;Keyboard;49.90\r\n2;Mouse;19.99\r\n", csv);

var result = CsvSerializer.Deserialize(csv, AppCsvContext.Default.Product);
Assert.Equal(products, result);
Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  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.  net11.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net10.0

    • No dependencies.
  • net11.0

    • No dependencies.

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
6.0.2 73 12/1/2026
6.0.1 121 11/29/2026
6.0.0 109 11/22/2026
5.0.4 169 11/8/2026
5.0.3 352 10/21/2026
5.0.2 391 9/6/2026
5.0.1 156 8/29/2026
5.0.0 538 7/5/2026
4.0.11 333 6/13/2026
4.0.10 336 5/14/2026
4.0.9 292 4/25/2026
4.0.8 681 1/18/2026
4.0.7 1,023 10/27/2025
4.0.6 236 10/19/2025
4.0.5 292 9/3/2025
4.0.4 275 11/17/2024
4.0.3 387 11/15/2023
4.0.2 695 7/14/2021
4.0.1 565 4/22/2021
4.0.0 714 9/24/2020
Loading failed