CsvSmartParser 1.0.0
dotnet add package CsvSmartParser --version 1.0.0
NuGet\Install-Package CsvSmartParser -Version 1.0.0
<PackageReference Include="CsvSmartParser" Version="1.0.0" />
<PackageVersion Include="CsvSmartParser" Version="1.0.0" />
<PackageReference Include="CsvSmartParser" />
paket add CsvSmartParser --version 1.0.0
#r "nuget: CsvSmartParser, 1.0.0"
#:package CsvSmartParser@1.0.0
#addin nuget:?package=CsvSmartParser&version=1.0.0
#tool nuget:?package=CsvSmartParser&version=1.0.0
CsvSmartParser
A robust, easy-to-use CSV parsing library for .NET 8.0 with auto-detection features, template-based parsing, and comprehensive validation.
Features
- 🚀 Async/Await Support: Fully asynchronous APIs using Task
- 🔍 Auto-Detection: Automatically detects file encoding and delimiters
- 📋 Template-Based Parsing: Type-safe parsing with customizable templates
- ✅ Validation: Built-in validation rules with custom validator support
- 🎯 Flexible: Parse from files, strings, or streams
- 📊 Large File Support: Streaming support for processing large CSV files
- 🛡️ Error Handling: Meaningful exceptions with detailed error messages
Installation
dotnet add package CsvSmartParser
Quick Start
Basic File Parsing
using CsvSmartParser;
var parser = new CsvParser();
// Parse from file with auto-detection
var path = Path.Combine(AppContext.BaseDirectory, "Data", "Year_End_Stock_Prices_2015_2024.csv");
var data = await parser.ParseFileAsync(path);
// Print basic information
Console.WriteLine($"Total rows: {data.Count}");
Console.WriteLine($"Columns: {string.Join(", ", data.FirstOrDefault()?.Keys ?? Array.Empty<string>())}");
Console.WriteLine();
// Print all data
foreach (var row in data)
{
foreach (var kvp in row)
{
Console.Write($"{kvp.Key}: {kvp.Value} | ");
}
Console.WriteLine();
}
String Parsing
using CsvSmartParser;
var parser = new CsvParser();
// Parse from string
var csvString = "Name,Age,City\nJohn,30,New York\nJane,25,Los Angeles";
var result = await parser.ParseStringAsync(csvString);
foreach (var row in result)
{
Console.WriteLine($"Name: {row["Name"]}, Age: {row["Age"]}, City: {row["City"]}");
}
Template-Based Validation
using CsvSmartParser;
using CsvSmartParser.Models;
// Create a validation template
var template = new CsvTemplate()
.AddStringColumn("Name", new CsvValidationRule
{
IsRequired = true,
MinLength = 2,
MaxLength = 50
})
.AddIntColumn("Age", new CsvValidationRule
{
IsRequired = true,
CustomValidator = value => int.TryParse(value, out var age) && age >= 0 && age <= 150,
ErrorMessage = "Age must be between 0 and 150"
})
.AddStringColumn("Email", new CsvValidationRule
{
IsRequired = true,
Pattern = @"^[^@\s]+@[^@\s]+\.[^@\s]+$",
ErrorMessage = "Please provide a valid email address"
});
var parser = new CsvParser();
try
{
var employees = await parser.ParseFileAsync("employees.csv", template);
Console.WriteLine($"Successfully validated {employees.Count} employees");
}
catch (CsvValidationException ex)
{
Console.WriteLine($"Validation failed: {ex.Message}");
}
Streaming Large Files
using CsvSmartParser;
var parser = new CsvParser();
// Process large files without loading everything into memory
using var fileStream = File.OpenRead("large-dataset.csv");
await foreach (var row in parser.ParseStreamAsync(fileStream))
{
// Process each row individually
Console.WriteLine($"Processing: {string.Join(", ", row.Values)}");
// Your processing logic here
await ProcessRowAsync(row);
}
async Task ProcessRowAsync(Dictionary<string, string> row)
{
// Simulate some async processing
await Task.Delay(10);
}
Custom Configuration
using CsvSmartParser;
using CsvSmartParser.Models;
var options = new CsvParsingOptions
{
Delimiter = ';', // Use semicolon as delimiter
HasHeaders = true, // First row contains headers
TrimWhitespace = true, // Trim spaces from values
SkipEmptyLines = true, // Skip empty lines
Encoding = Encoding.UTF8 // Specify encoding
};
var parser = new CsvParser(options);
var result = await parser.ParseFileAsync("data.csv");
Advanced Features
Auto-Detection Capabilities
The library automatically detects:
- Encodings: UTF-8 (with/without BOM), UTF-16 LE/BE, UTF-32 LE/BE
- Delimiters: Comma (,), Semicolon (;), Tab (\t), Pipe (|), Colon (:)
- Headers: Automatic detection of header rows
Validation Rules
var template = new CsvTemplate()
.AddStringColumn("ProductName", new CsvValidationRule
{
IsRequired = true,
MinLength = 3,
MaxLength = 100,
Pattern = @"^[A-Za-z0-9\s\-]+$",
ErrorMessage = "Product name can only contain letters, numbers, spaces, and hyphens"
})
.AddDecimalColumn("Price", new CsvValidationRule
{
IsRequired = true,
CustomValidator = value => decimal.TryParse(value, out var price) && price > 0,
ErrorMessage = "Price must be a positive number"
})
.AddDateTimeColumn("LaunchDate", new CsvValidationRule
{
IsRequired = false,
CustomValidator = value => string.IsNullOrEmpty(value) ||
DateTime.TryParse(value, out var date) && date <= DateTime.Now,
ErrorMessage = "Launch date cannot be in the future"
});
Error Handling
try
{
var data = await parser.ParseFileAsync("data.csv", template);
}
catch (FileNotFoundException ex)
{
Console.WriteLine($"File not found: {ex.Message}");
}
catch (CsvParsingException ex)
{
Console.WriteLine($"CSV parsing error at row {ex.RowNumber}: {ex.Message}");
}
catch (CsvValidationException ex)
{
Console.WriteLine($"Validation failed: {ex.Message}");
foreach (var error in ex.ValidationErrors)
{
Console.WriteLine($" Row {error.RowNumber}, Column '{error.ColumnName}': {error.ErrorMessage}");
}
}
API Reference
CsvParser Methods
ParseFileAsync(string filePath, CsvTemplate? template = null, Encoding? encoding = null)
- Parse CSV file with optional template validationParseStringAsync(string csvData, CsvTemplate? template = null)
- Parse CSV from string dataParseStreamAsync(Stream stream, CsvTemplate? template = null, Encoding? encoding = null)
- Stream large CSV files
CsvTemplate Methods
AddStringColumn(string name, CsvValidationRule? rule = null)
- Add string column with validationAddIntColumn(string name, CsvValidationRule? rule = null)
- Add integer column with validationAddDecimalColumn(string name, CsvValidationRule? rule = null)
- Add decimal column with validationAddDateTimeColumn(string name, CsvValidationRule? rule = null)
- Add DateTime column with validationAddBooleanColumn(string name, CsvValidationRule? rule = null)
- Add boolean column with validation
Requirements
- .NET 8.0 or later
- No external dependencies
License
MIT License - see LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
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. 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. |
-
net8.0
- System.Text.Encoding.CodePages (>= 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.
Version | Downloads | Last Updated |
---|---|---|
1.0.0 | 133 | 9/13/2025 |
## Version 1.0.0 - Initial Release
### 🎉 Features
- **Async/Await Support**: Fully asynchronous APIs using Task-based operations
- **Auto-Detection**: Automatically detects file encoding (UTF-8, UTF-16, UTF-32) and delimiters (comma, semicolon, tab, pipe, colon)
- **Template-Based Parsing**: Type-safe parsing with customizable validation templates
- **Comprehensive Validation**: Built-in validation rules with support for custom validators
- **Multiple Input Sources**: Parse from files, strings, or streams
- **Large File Support**: Streaming support for processing large CSV files without memory issues
- **Error Handling**: Meaningful exceptions with detailed error messages and context
### 🔧 Core Components
- `CsvParser`: Main parsing class with auto-detection capabilities
- `CsvTemplate`: Template-based validation and type mapping
- `CsvParsingOptions`: Flexible configuration options
- `CsvValidationRule`: Comprehensive validation rule system
### 📊 Supported Data Types
- String with length validation and pattern matching
- Integer and decimal number validation
- DateTime parsing with multiple format support
- Boolean value parsing
- Custom type validation through delegates
### 🚀 Performance Features
- Memory-efficient streaming for large files
- Optimized parsing algorithms
- Minimal memory allocation during processing
- Support for files of any size
### 🛡️ Validation Features
- Required field validation
- String length constraints (min/max)
- Regular expression pattern matching
- Custom validation functions
- Detailed error messages with row/column context
### 📝 API Methods
- `ParseFileAsync(string filePath, CsvTemplate? template = null, Encoding? encoding = null)`
- `ParseStringAsync(string csvData, CsvTemplate? template = null)`
- `ParseStreamAsync(Stream stream, CsvTemplate? template = null, Encoding? encoding = null)`
### 🔍 Auto-Detection Capabilities
- **Encoding Detection**: UTF-8 (with/without BOM), UTF-16 LE/BE, UTF-32 LE/BE, system default
- **Delimiter Detection**: Comma (,), Semicolon (;), Tab (\t), Pipe (|), Colon (:)
- **Header Detection**: Automatic detection of header rows
- **Quote Handling**: Proper handling of quoted values with embedded delimiters and quotes
### 📦 Requirements
- .NET 8.0 or later
- No external dependencies
### 🎯 Use Cases
- Data import/export operations
- ETL (Extract, Transform, Load) processes
- Data validation and cleansing
- Report generation from CSV data
- Migration scripts and data conversion
- Financial and business data processing
### 🔧 Configuration Options
- Custom delimiters
- Header row configuration
- Whitespace trimming
- Empty line handling
- Encoding specification
- Validation rule customization
This initial release provides a solid foundation for CSV parsing in .NET applications with enterprise-grade features and performance.