CsvProcessor 1.0.2.1

dotnet add package CsvProcessor --version 1.0.2.1
NuGet\Install-Package CsvProcessor -Version 1.0.2.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="CsvProcessor" Version="1.0.2.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add CsvProcessor --version 1.0.2.1
#r "nuget: CsvProcessor, 1.0.2.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.
// Install CsvProcessor as a Cake Addin
#addin nuget:?package=CsvProcessor&version=1.0.2.1

// Install CsvProcessor as a Cake Tool
#tool nuget:?package=CsvProcessor&version=1.0.2.1

CSV Processor

CSV Processor is a library that allows the processing CSV files.

Features

  • Convert file to a DataTable
  • Convert file to a List of .NET object
  • Custom Processor with DataTable as input
  • Define Column position with a custom .NET Attribute [csv(position: "0")]

Getting Started

Installing from NuGet

Supported platforms: .NET Core 3.1

dotnet add package CsvProcessor

Browse the <a href="https://www.nuget.org/packages/CsvProcessor/">CsvProcessor on NuGet</a>.

Setup

Types are in the CsvProcessor namespace

using CsvProcessor;

The root Processor is created using CsvProcessor.Processor.

string path = "[CSV File Directory]";
Processor<FileTypes> processor = new Processor<FileTypes>(path, FileTypes.Countries);

Note that FileType could be any enumerables that you have set in your project.

Usage

There are 3 ways to use treat a CSV file with the CsvProcessor

  • Converting the file to an IEnumerable of .NET Object.
  • Converting the file to a DataTable.
  • Defining a custom processor.
Converting the file to an IEnumerable of .NET Object

Step 1: Defining the DataModel (Example will be using an ISO standard CSV)

public class Country
{
    [Csv("0")]
    public string Name { get; set; }
    
    [Csv("1")]
    public string AlphaCode2 { get; set; }
    
    [Csv("2")]
    public string AlphaCode3 { get; set; }
    
    [Csv("3")]
    public int NumericalCode { get; set; }
    
    [Csv("4")]
    public double Latitude { get; set; }
    
    [Csv("5")]
    public double Longitude { get; set; }
}

_Note that we make use of the CsvAttribute, which takes only one parameter: Position, to define the column position of the different properties in the CSV File.

Step 2: Converting the CSV File to a IEnumerable<Country> which can later be converted to a List<Country> afterward if there's ever a need.

string path = "[CSV File Directory]";
Processor<FileTypes> processor = new Processor<FileTypes>(path, FileTypes.Countries);
IEnumerable<Country> countries = processor.Execute<Country>();

//Optional to List
List<Country> countries = processor.Execute<Country>().ToList();
Converting the file to a DataTable
string path = "[CSV File Directory]";
Processor<FileTypes> processor = new Processor<FileTypes>(path, FileTypes.Countries);
DataTable dt = processor.GetContentAsDataTable();
Defining a custom processor

Step 1: Defining the custom processor by inheriting the interface IProcessor which will automatically convert the file into a DataTable and will append Column with the column Index. For example, the first column in the file will be named Column0.

public class CountryProcessor : IProcessor
{
  void IProcessor.Process(DataTable dt)
  {
    // Processor Logic after DataTable conversion
    foreach (DataRow row in datatable.Rows)
    {
      Country country = new Country()
      {
          Name = row["Column0"].ToString(),
          AlphaCode2 = row["Column1"].ToString(),
          AlphaCode3 = row["Column2"].ToString(),
          NumericalCode = Convert.ToInt32(row["Column3"].ToString()),
          Latitude = Convert.ToDouble(row["Column4"].ToString()),
          Longitude = Convert.ToDouble(row["Column5"].ToString())
      };
    }
  }
}

Step 2: Processing the file

string path = "[CSV File Directory]";

FileTypeInit.FileTypes.Add(FileTypes.Countries, new CountryProcessor());

Processor<FileTypes> processor = new Processor<FileTypes>(path, FileTypes.Countries);
processor.Execute();

Licensing

CSV Processor is open source under the MIT license and is free for commercial use.

Thanks

Thanks to Shayl Sawmynaden for pitching the idea.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.1 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETCoreApp 3.1

    • 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
1.0.2.1 438 4/8/2022
1.0.1 667 2/7/2022

Support for CSV files with header