FG.CsvParser: A Powerful .NET Library for Working with CSV Files
Working with CSV files is a common task for developers. FG.CsvParser is a .NET Standard 2.1 library designed to make reading, writing, and querying CSV files simple and efficient. With its flexible API and extensive configuration options, FG.CsvParser is the perfect tool for handling your CSV-related tasks.
Key Features of FG.CsvParser
- Read CSV Files: Convert CSV content into JSON or a list of strongly-typed objects.
- Write CSV Files: Easily write CSV content to files or append to existing ones.
- Highly Configurable: Adjust parsing options such as delimiters, row splitters, and encoding to suit your needs.
- Query CSV Data: Use custom filters to query CSV content efficiently.
Installation
To start using FG.CsvParser, you can add it to your project via NuGet:
dotnet add package FG.CsvParser
Getting Started with FG.CsvParser
Creating a CsvParser Instance
FG.CsvParser allows you to create a parser instance in multiple ways. Here are some examples:
Open a CSV File with Default Settings
var parser = CsvParser.OpenFile("path/to/your/file.csv");
Open a CSV File with a Header Row
var parser = CsvParser.OpenFile("path/to/your/file.csv", hasHeader: true);
Open a CSV File with Custom Configuration
var configuration = new CsvParserConfiguration
{
HasHeader = true,
Delimitter = ',',
RowSplitter = "\n",
Encoding = Encoding.UTF8
};
var parser = CsvParser.OpenFile("path/to/your/file.csv", configuration);
Reading CSV Content
Convert CSV Content to JSON
using var parser = CsvParser.OpenFile("path/to/your/csvfile.csv", new CsvParserConfiguration
{
HasHeader = true,
Delimitter = ',',
RowSplitter = "\r\n",
Encoding = Encoding.UTF8
});
string? jsonContent = await parser.ReadAsJson();
Console.WriteLine(jsonContent);
Convert CSV Content to a List of Objects
using var parser = CsvParser.OpenFile("path/to/your/csvfile.csv", 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
Write CSV Content as a String
using var parser = CsvParser.OpenFile("path/to/your/file.csv", 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.");
Write a List of Objects to CSV
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.");
Querying CSV Content
You can query CSV data using custom filters to extract only the information you need.
using var parser = CsvParser.OpenFile("path/to/your/csvfile.csv", 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 Options
The CsvParserConfiguration
class provides extensive configuration options to customize your CSV parsing experience:
var configuration = new CsvParserConfiguration
{
HasHeader = true,
Delimitter = ';',
RowSplitter = "\n",
Encoding = Encoding.UTF8
};
using var parser = CsvParser.OpenFile("path/to/your/file.csv", configuration);
Why Choose FG.CsvParser?
FG.CsvParser simplifies working with CSV files by providing a clean and intuitive API. Whether you need to process CSV files for small tasks or large-scale applications, FG.CsvParser offers the tools and flexibility required to handle your data efficiently.
Install FG.CsvParser today and take control of your CSV workflows!
Top comments (0)