CsvReaderAdvanced 2.7.1

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

CsvReaderAdvanced

A CSV parsing library for .NET with DI-friendly setup, schema-aware header mapping, lazy/full reading modes, and typed value parsing.

Install

Package Manager:

Install-Package CsvReaderAdvanced

.NET CLI:

dotnet add package CsvReaderAdvanced

Quick start (DI)

using CsvReaderAdvanced;
using Microsoft.Extensions.Hosting;

var host = Host.CreateDefaultBuilder(args)
    .ConfigureServices((context, services) =>
    {
        services.AddCsvReader(context.Configuration);
    })
    .Build();

var fileFactory = host.Services.GetCsvFileFactory();

AddCsvReader registers:

  • CsvReader
  • CsvFileFactory
  • CsvSchemaOptions from the csvSchemas config section

Configure schemas in appsettings.json

{
  "csvSchemas": {
    "schemas": [
      {
        "name": "products",
        "fields": [
          {
            "name": "ProductID",
            "alternatives": ["Product ID"],
            "required": true
          },
          {
            "name": "Weight",
            "unit": "t",
            "alternativeFields": ["Volume", "TEU"],
            "required": true
          },
          {
            "name": "Volume",
            "unit": "m^3",
            "alternativeUnits": ["m3", "m^3"]
          }
        ]
      }
    ]
  }
}

Read a file

Use CsvFileFactory to create a CsvFile:

var file = fileFactory.ReadWholeFile(path, Encoding.UTF8, withHeader: true);
// or:
var lazyFile = fileFactory.GetFile(path, Encoding.UTF8, withHeader: true);
  • ReadWholeFile loads all rows into file.Lines
  • GetFile reads header metadata and lets you stream rows with Read(...)

When withHeader: true, ReadHeader() populates:

  • Header
  • ExistingColumns (Dictionary<string,int>, case-insensitive)

Validate header against a schema

var options = host.Services.GetSchemaOptions();
var schema = options.Schemas?.FirstOrDefault(s => s.Name == "products");

if (schema is null)
    throw new InvalidOperationException("Schema 'products' was not found.");

var file = fileFactory.GetFile(path, Encoding.UTF8, withHeader: true);
file.CheckAgainstSchema(schema);

if (file.MissingRequiredFields.Any())
{
    foreach (var missing in file.MissingRequiredFields)
        Console.WriteLine($"Missing required field: {missing.Name}");
}

// schema field name -> column index
var columns = file.ExistingFieldColumns;

CheckAgainstSchema updates:

  • ExistingFieldColumns
  • MissingFields
  • MissingRequiredFields

Parsed values and line information

Each parsed row is a TokenizedLine with:

  • Tokens
  • FromLine / ToLine (helpful when quoted values span multiple physical lines)

Typed getters such as GetDouble, GetInt, GetDateTime, GetBoolean, etc. return ParsedValue<T> with:

  • Value
  • State (Parsed, Null, Unparsable, NaN)
  • IsParsed, IsNull, IsNaN
  • StringValue
var file = fileFactory.ReadWholeFile(path, Encoding.UTF8, withHeader: true);
var c = file.ExistingColumns;

foreach (var line in file.Lines!.Where(l => l.HasValue).Select(l => l!.Value))
{
    var productName = line.GetString("ProductName", c);
    var weight = line.GetDouble("Weight", c);

    if (!weight.IsParsed)
    {
        Console.WriteLine($"Invalid weight '{weight.StringValue}' at line {line.FromLine}");
        continue;
    }

    double value = weight;   // implicit conversion
    double? nullable = weight;
}

Example: lazy-read for low memory usage

var file = fileFactory.GetFile(path, Encoding.UTF8, withHeader: true);
var c = file.ExistingColumns;

foreach (var line in file.Read(skipHeader: true))
{
    if (!line.HasValue) continue;

    var t = line.Value;
    var id = t.GetInt("ProductID", c);
    var qty = t.GetLong("Quantity", c);

    if (id.IsParsed && qty.IsParsed)
    {
        // process row
    }
}

Example: detect types and compute stats

var file = fileFactory.GetFile(path, Encoding.UTF8, withHeader: true);

// Infer base types from data
file.UpdateFieldBaseTypes(maxRows: 5000);

// Then compute stats for inferred types
file.UpdateFieldStats(maxRows: 5000);

foreach (var f in file.ExistingFieldTypeInfos)
{
    Console.WriteLine($"Column {f.Column}: {f.BaseType}, Nulls={f.NullValuesCount}, Unparsed={f.UnparsedValuesCount}, Min={f.Minimum}, Max={f.Maximum}");
}

Example: export selected columns

var file = fileFactory.GetFile(path, Encoding.UTF8, withHeader: true);

await file.SavePartialAs(
    targetPath: @".\\samples\\export.csv",
    targetSeparator: ';',
    columnNames: new[] { "ProductID", "ProductName", "Weight" });

You can also export by index:

await file.SavePartialAs(@".\\samples\\export.csv", ';', 0, 2, 5);

Notes

  • Separator detection supports ;, ,, and tab.
  • ReadFast(...) / fast tokenization APIs are available for simpler inputs where quoted multiline handling is not needed.
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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on CsvReaderAdvanced:

Package Downloads
SqlServerExplorerLib

The easiest way to perform SQL Server operations, such as viewing table fields, copying table data to a file, or transferring table data between databases. See the README for more information.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.7.1 97 7/15/2026
2.6.0 492 2/23/2025
2.5.0 249 1/17/2025
2.4.1 226 12/27/2024
2.4.0 445 11/15/2024
2.3.8 428 10/16/2024
2.3.7 312 7/27/2024
2.3.6 253 7/27/2024
2.3.5 221 7/26/2024
2.3.4 254 7/12/2024
2.3.3 266 6/22/2024
2.3.2 297 6/22/2024
2.3.1 923 3/1/2024
2.3.0 403 12/13/2023
2.2.1 262 11/28/2023
2.2.0 326 10/17/2023
2.1.1 271 10/15/2023
2.1.0 243 10/15/2023
1.3.3 250 10/14/2023
1.3.2 263 10/14/2023
Loading failed