FG.CsvParser: A Powerful .NET Library for Working with CSV Files

Leave a Comment

For developers, working with CSV files is a routine activity. A.NET Standard 2.1 library called FG.CsvParser was created to simplify and expedite the reading, writing, and querying of CSV files. The ideal solution for managing your CSV-related duties is FG.CsvParser because of its broad customizable options and versatile API.


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 for 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.

Windows Hosting Recommendation

HostForLIFEASP.NET receives Spotlight standing advantage award for providing recommended, cheap and fast ecommerce Hosting including the latest Magento. From the leading technology company, Microsoft. All the servers are equipped with the newest Windows Server 2022 R2, SQL Server 2022, ASP.NET Core 7.0.10 , ASP.NET MVC, Silverlight 5, WebMatrix and Visual Studio Lightswitch. Security and performance are at the core of their Magento hosting operations to confirm every website and/or application hosted on their servers is highly secured and performs at optimum level. mutually of the European ASP.NET hosting suppliers, HostForLIFE guarantees 99.9% uptime and fast loading speed. From €3.49/month , HostForLIFE provides you with unlimited disk space, unlimited domains, unlimited bandwidth,etc, for your website hosting needs.
 
https://hostforlifeasp.net/

Next PostNewer Post Previous PostOlder Post Home

0 comments:

Post a Comment