FG.CsvParser
1.0.6
See the version list below for details.
dotnet add package FG.CsvParser --version 1.0.6
NuGet\Install-Package FG.CsvParser -Version 1.0.6
<PackageReference Include="FG.CsvParser" Version="1.0.6" />
paket add FG.CsvParser --version 1.0.6
#r "nuget: FG.CsvParser, 1.0.6"
// Install FG.CsvParser as a Cake Addin #addin nuget:?package=FG.CsvParser&version=1.0.6 // Install FG.CsvParser as a Cake Tool #tool nuget:?package=FG.CsvParser&version=1.0.6
FG.CsvParser
FG.CsvParser is a .NET Standard 2.1 library for parsing and writing CSV files. It provides a flexible and easy-to-use API for reading, writing, and querying CSV data.
Features
- Read CSV files and convert them to JSON or a list of objects.
- Write CSV content to files.
- Configure CSV parsing options such as delimiter, row splitter, and encoding.
- Query CSV files with custom filters.
Installation
To install FG.CsvParser, add the following package to your project:
Add the library to your project via NuGet:
dotnet add package FG.CsvParser
Usage
Creating a CsvParser Instance
You can create a CsvParser
instance using one of the static OpenFile
methods:
- Open a CSV file with default settings:
var parser = CsvParser.OpenFile("path/to/your/file.csv");
- Open a CSV file and specify if it has a header row:
var parser = CsvParser.OpenFile("path/to/your/file.csv", hasHeader: true);
- Open a CSV file with a custom configuration:
var configuration = new CsvParserConfiguration
{
HasHeader = true,
Delimitter = ',',
RowSplitter = "
",
Encoding = Encoding.UTF8
};
var parser = CsvParser.OpenFile("path/to/your/file.csv", configuration);
Reading CSV Content
You can read the CSV content and convert it to JSON or a list of objects:
- Reading CSV Content as JSON
var filePath = "path/to/your/csvfile.csv";
using var parser = CsvParser.OpenFile(filePath, new CsvParserConfiguration { HasHeader = true, Delimitter = ',', RowSplitter = "\r\n", Encoding = Encoding.UTF8 });
string? jsonContent = await parser.ReadAsJson();
Console.WriteLine(jsonContent);
- Reading CSV Content as a List of Objects
var filePath = "path/to/your/csvfile.csv";
using var parser = CsvParser.OpenFile(filePath, new CsvParserConfiguration { HasHeader = true, Delimitter = ',', RowSplitter = "\r\n", Encoding = Encoding.UTF8 });
List<MyDataClass> dataList = await parser.ReadAs<MyDataClass>();
foreach (var data in dataList)
{
Console.WriteLine(data);
}
public class MyDataClass
{
public string Name { get; set; }
[CsvColumn("Home address")]
public string HomeAddress { get; set; }
}
Writing CSV Content
You can write CSV content to a file:
- Writing CSV Content as a String
var filePath = "path/to/your/csvfile.csv";
using var parser = CsvParser.OpenFile(filePath, new CsvParserConfiguration { HasHeader = true, Delimitter = ',', RowSplitter = "\r\n", Encoding = Encoding.UTF8 });
string csvContent = "Column1,Column2\nValue1,Value2\nValue3,Value4";
await parser.WriteAsync(csvContent, append: false);
Console.WriteLine("CSV content written to file.");
- Writing a List of Objects to CSV
var filePath = "path/to/your/csvfile.csv";
using var parser = CsvParser.OpenFile(filePath, new CsvParserConfiguration { HasHeader = true, Delimitter = ',', RowSplitter = "\r\n", Encoding = Encoding.UTF8 });
var dataList = new List<MyDataClass>
{
new MyDataClass { Column1 = "Value1", Column2 = 1 },
new MyDataClass { Column1 = "Value2", Column2 = 2 }
};
await parser.WriteAsync(dataList, append: false);
Console.WriteLine("List of objects written to CSV file.");
- Appending CSV Content
var filePath = "path/to/your/csvfile.csv";
using var parser = CsvParser.OpenFile(filePath, new CsvParserConfiguration { HasHeader = true, Delimitter = ',', RowSplitter = "\r\n", Encoding = Encoding.UTF8 });
string csvContent = "Value5,Value6\nValue7,Value8";
await parser.WriteAsync(csvContent, append: true);
Console.WriteLine("CSV content appended to file.");
Querying CSV Content
You can query the CSV content with a custom filter:
var filePath = "path/to/your/csvfile.csv";
using var parser = CsvParser.OpenFile(filePath, new CsvParserConfiguration { HasHeader = true, Delimitter = ',', RowSplitter = "\r\n", Encoding = Encoding.UTF8 });
await foreach (var item in parser.Query<MyDataClass>(data => data.Column2 > 100))
{
Console.WriteLine(item);
}
Configuration
The CsvParserConfiguration
class allows you to configure various options for the CSV parser:
var filePath = "path/to/your/csvfile.csv";
// Create a configuration for the CSV parser
var configuration = new CsvParserConfiguration
{
HasHeader = true,
Delimitter = ';',
RowSplitter = "\n",
Encoding = Encoding.UTF8
};
// Open the CSV file with the specified configuration
using var parser = CsvParser.OpenFile(filePath, configuration);
// Example: Reading CSV content as JSON
string? jsonContent = await parser.ReadAsJson();
Console.WriteLine("CSV content as JSON:");
Console.WriteLine(jsonContent);
// Example: Writing CSV content
var dataList = new List<MyDataClass>
{
new MyDataClass { Column1 = "Value1", Column2 = 1 },
new MyDataClass { Column1 = "Value2", Column2 = 2 }
};
await parser.WriteAsync(dataList, append: false);
Console.WriteLine("List of objects written to CSV file.");
License
This project is licensed under the MIT License. See the LICENSE file for details.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. 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. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.1)
- System.Text.Json (>= 9.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.