Dbarone.Net.Csv 2.0.0

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

Dbarone.Net.Csv

Dbarone.Net.Csv is a simple CSV library that provides a CsvReader and CsvWriter class to read and write csv files. The parsing of csv is compliant to https://www.rfc-editor.org/rfc/rfc4180, and a full suite of unit tests is provided to validate the code.

CsvReader

The CsvReader class provides a single Read() method to read csv files from any Stream object. The Read() method returns a sequence of IDictionary<string, object> objects

    var input = @"field_name1,field_name2,field_name3
    aaa,bbb,ccc
    zzz,yyy,xxx";

    byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(input);
    MemoryStream stream = new MemoryStream(byteArray);
    CsvReader csv = new CsvReader(stream);
    var results = csv.Read();

The Read() method will yield records. Note that if you want to read the entire csv file into memory, you will need to make a call such as ToList().

Configuration

By default, the CsvReader and CsvWriter class use the following defaults:

Item Value
Field Separator ,
Field Escape "
Line Separator LF on Unix systems, CRLF on other systems

However, these settings can be configured in the CsvConfiguration object which is passed in the constructor to the CsvReader or CsvWriter. The following configuration is available:

Item Description
FieldSeparator Sets the field separator character. Defaults to ','
FieldEscape Sets the field escape character. Defaults to '"'
LineSeparator Sets the line separator string. Defaults to LF on Unix systems, CRLF on other systems.
HasHeader Set to true if the csv file has headers. If no headers are present, headers 'Column1', 'Column2',... will be generated
Headers Used to override the headers in the file or data.
Culture Sets the CultureInfo instance to define how values are formatted to / from strings
InvalidRowHandler You can define a custom callback function to process invalid data rows (including empty lines) that may be read from the csv file.

CsvWriter

Csv files can be created using the CsvWriter class. The Write() method will write a sequence of IDictionary<string, object> objects to a stream.

    IEnumerable<Dictionary<string, object>> data = new IEnumerable<Dictionary<string, object>>();
    Dictionary<string, object> dict = new Dictionary<string, object>();
    dict.Add("foo", "123");
    dict.Add("bar", "456");
    dict.Add("baz", "789");
    data.Add(dict);

    MemoryStream ms = new MemoryStream();
    CsvWriter csv = new CsvWriter(ms, configuration);
    csv.Write(data);

Reading Csv Files to Strong-Typed objects

This library deals purely with parsing of CSV files. Csv files by nature are only text, and have no built-in information about types. This is why the Read() method returns a sequence of IDictionary<string, object> objects. By default, it does not make any assumptions about the type of data being read and returns all values as string values. If you need to cast or manipuate values, you can do this by implementing a RowProcess callback function on the CsvReader class.

    // This callback function converts the 1st column to an integer
    ProcessRowDelegate mapColumn = (int record, string[] headers, ref object[]? values) =>
    {
        int newValue = int.Parse((string)values![0]);
        values[0] = newValue;
        return true;
    };

    CsvReader csv = new CsvReader(stream, new CsvConfiguration { ProcessRowHandler = mapColumn });

To map records to classes, you can use the Dbarone.Net.Mapper library in conjunction with this library.

The API reference documentation can be found here: Dbarone.Net.Csv.html.

--- d.barone 3-Mar-2024 ---

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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.  net9.0 was computed.  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.
  • net8.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Dbarone.Net.Csv:

Package Downloads
Dbarone.Net.Fake

A .NET fake data generator library.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.0.0 143 6/9/2024
1.0.1 203 3/9/2024
1.0.0 142 3/8/2024