Carubbi.TextFile
1.2.0
dotnet add package Carubbi.TextFile --version 1.2.0
NuGet\Install-Package Carubbi.TextFile -Version 1.2.0
<PackageReference Include="Carubbi.TextFile" Version="1.2.0" />
paket add Carubbi.TextFile --version 1.2.0
#r "nuget: Carubbi.TextFile, 1.2.0"
// Install Carubbi.TextFile as a Cake Addin #addin nuget:?package=Carubbi.TextFile&version=1.2.0 // Install Carubbi.TextFile as a Cake Tool #tool nuget:?package=Carubbi.TextFile&version=1.2.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:
- Define the Record Class:
[Delimiter(',')]
public class RecordExample
{
[DelimiterField(1)]
[PositionalField(0, 20)]
public string Name { get; set; } = null!;
[DelimiterField(2)]
[PositionalField(20, 10)]
public DateTime? DateOfBirth { get; set; }
[DelimiterField(3)]
[PositionalField(30, 2)]
public int? Children { get; set; }
}
- 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 reader = new FlatTextFileReader<RecordExample>(new ReadingOptions { SkipHeader = true, Mode = ContentMode.Delimited });
var result = reader.ReadFile("test1.txt");
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).InPositionalIndex(0, 20);
Property(x => x.DateOfBirth).InDelimitedOrder(2).InPositionalIndex(20, 10);
Property(x => x.Children).InDelimitedOrder(3).InPositionalIndex(30, 2);
}
}
Enjoy using Carubbi.TextFile for your text parsing needs!
Product | Versions 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. |
-
net8.0
- System.Diagnostics.PerformanceCounter (>= 8.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.