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.
- 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.
To install FG.CsvParser, add the following package to your project:
Add the library to your project via NuGet:
dotnet add package FG.CsvParserYou 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);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; }
}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.");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);
}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.");This project is licensed under the MIT License. See the LICENSE file for details.