Carubbi.TextFile 1.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Carubbi.TextFile --version 1.1.0                
NuGet\Install-Package Carubbi.TextFile -Version 1.1.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="Carubbi.TextFile" Version="1.1.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Carubbi.TextFile --version 1.1.0                
#r "nuget: Carubbi.TextFile, 1.1.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.
// Install Carubbi.TextFile as a Cake Addin
#addin nuget:?package=Carubbi.TextFile&version=1.1.0

// Install Carubbi.TextFile as a Cake Tool
#tool nuget:?package=Carubbi.TextFile&version=1.1.0                

Carubbi.TextFile

Carubbi.TextFile is a text parser library designed to facilitate reading and writing text and CSV files with support for positional layouts, delimited-based layouts, and hierarchical layouts. Configuration is flexible and can be done via attributes or a fluent API.

Features

  • Positional Layouts: Define fixed-width fields in text files.
  • Delimited Layouts: Define fields separated by specific delimiters, such as commas or tabs.
  • Hierarchical Layouts: Handle nested or hierarchical data structures.
  • Configuration Options: Use attributes or fluent API for configuration.
  • Skipping Headers: Easily skip headers in files.

Installation

Add the Carubbi.TextFile package to your project using NuGet:

dotnet add package Carubbi.TextFile

Usage

Example

Consider the following CSV content:

Name,DOB,Children
Raphael Carubbi Neto,29/09/1981,2
John Doe,,1
Bob B.,30/05/2002

Save this content to a file named test1.txt.

Reading a Delimited File

To read the CSV file using Carubbi.TextFile, follow these steps:

  1. Define the Record Class:
[Delimiter(',')]
public class RecordExample
{
    [DelimiterField(1)]
    public string Name { get; set; } = null!;

    [DelimiterField(2)]
    public DateTime? DateOfBirth { get; set; }

    [DelimiterField(3)]
    public int? Children { get; set; }
}
  1. Read the file:
var fileContent = """
                   Name,DOB,Children
                   Raphael Carubbi Neto,29/09/1981,2
                   John Doe,,1
                   Bob B.,30/05/2002
                   """;

File.WriteAllText("test1.txt", fileContent);

var result = FlatTextFileReader.ReadFile<RecordExample>("test1.txt", new ReadingOptions { SkipHeader = true, Mode = ContentMode.Delimited });

Configuration Options

  • SkipHeader: Indicates whether to skip the first line (header) in the file.
  • Mode: Defines the content mode (ContentMode.Delimited for CSV files or ContentMode.Positional for fixed-width files).

Attributes

[Delimiter(char delimiter)]: Specifies the delimiter character for delimited files. [DelimiterField(int fieldIndex)]: Indicates the position of the field in a delimited file. [PositionalField(int startIndex, int length)]: Specifies the start index and length of the field in a positional file.

Fluent API (Alternative Configuration)

Instead of using attributes, you can configure the parsing logic using the fluent API:

public class FluentRecordExample
{
    public string Name { get; set; } = null!;

    public DateTime? DateOfBirth { get; set; }

    public int? Children { get; set; }
}
public class FluentRecordExampleConfiguration : TextFileRecordTypeConfiguration<FluentRecordExample>
{
    public FluentRecordExampleConfiguration()
    {
        HasDelimiter(',');
        Property(x => x.Name).InDelimitedOrder(1);
        Property(x => x.DateOfBirth).InDelimitedOrder(2);
        Property(x => x.Children).InDelimitedOrder(3);
    }
}

For more usage examples check the test project

Enjoy using Carubbi.TextFile for your text parsing needs!

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. 
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
1.2.0 339 7/21/2024
1.1.0 95 7/15/2024
1.0.0 100 7/8/2024