Getting Started
Installation
Install FlameCsv using the NuGet package manager:
dotnet add package FlameCsv
Basic Usage
Reading CSV data is as simple as:
// Reading from a string
IEnumerable<User> users = CsvReader.Read<User>("id,name\n1,John\n2,Jane");
// Reading from a file
await foreach (User user in CsvReader.ReadFromFile<User>("users.csv").WithCancellation(cancellationToken))
{
Console.WriteLine(user.Name);
}
// Peeking fields directly from the underlying data
double sum = 0;
foreach (CsvRecordRef<byte> record in new CsvReader<byte>(options, (ReadOnlyMemory<byte>)csv).ParseRecords())
{
ReadOnlySpan<byte> field = record[3];
if (double.TryParse(field, out double value))
{
sum += value;
}
}
Writing CSV is just as easy:
var users = new[]
{
new User { Id = 1, Name = "John" },
new User { Id = 2, Name = "Jane" }
};
// Writing to a string
StringBuilder csv = CsvWriter.WriteToString(users);
// Writing to a file
await CsvWriter.WriteToFileAsync("users.csv", users, cancellationToken);
Configuration
FlameCsv is highly configurable. Common options include:
- CSV dialect (delimiters, quotes) - see Configuration
- Header mapping - see Attributes
- Type conversion - see Configuration
Example of custom configuration:
CsvOptions<char> options = new()
{
Delimiter = ';',
Quote = '"',
Trimming = CsvFieldTrimming.Leading,
HasHeader = true,
Comparer = StringComparer.Ordinal,
};
The configuration object is identical for char
and byte
, and UTF16 <-> UTF8 conversion is handled automatically.
Only differences are the converters, which are separate for char
and byte
.
Next Steps
- See Examples for more detailed examples
- Learn about configuring types in Attributes
- Browse available configuration options in Configuration
- Check out the performance Benchmarks
- Understand the internals and design philosophy in Architecture
Comparisons to other libraries
FlameCsv | CsvHelper | Sylvan | Sep | RecordParser | |
---|---|---|---|---|---|
License | Apache 2.0 | MS-PL / Apache 2.0 | MIT | MIT | MIT |
Performance | 🐇 Fast | 🐌 Slow | 🐇 Fast | 🐇 Fast | 🐟 Moderate |
Memory use | 😎 Near-zero | 🤯 High | 🤐 Moderate | 😌 Low | 🤯 High |
Async support | ✔️ Yes | ✔️ Yes | ✔️ Yes | 〽️ Partial | ❌ No |
Type binding | ✔️ Yes | ✔️ Yes | ✔️ Yes | ❌ No | ❌ No |
AOT compatible | ✔️ Yes | ❌ No | ❌ No | ✔️ Yes | ❌ No |
Broken data support | ❌ No | ✔️ Yes | ✔️ Yes | ✔️ Yes | ❔ Unknown |