Read CSV files in C# .NET Core with examples

Read CSV filesTinyCSVParser CsvHelper or TextFieldParser

Today I am going to talk about a few simple approaches to Read CSV files in C# .NET Core framework.

We will cover multiple approaches to dealing with CSV files in C# .NET project using libraries like TinyCSVParser, CsvHelper, or TextFieldParser

We will see how to use an easy-to-use parser and convert CSV into JSON or other formats as required.

Today in this article we will cover the following aspects,

It is possible that while dealing with modern frameworks and technologies you will still need to use traditional file systems like CSV(.csv), Excel(.xlsx), Doc(.doc) or Text files(.txt).

These file systems are popular and useful means of persisting the application data.

Getting Started

Let’s create a .NET Core application.

To keep simple I have used the .NET Core console application.

I have used the .NET Core 3.1 version but the below example works in any .NET Core frameworks.

C CSV parser

Using CSV Parser?

Today in this article, we will cover below simple to use the parser,

Read CSV Using TinyCSVParser

TinyCSVParser is one of the simple and lightweight parsers for CSV Reading and parsing.

This package is available through Nuget Manager,

PM> Install-Package TinyCsvParser

It lets you parse CSV and helps you deserialize the data into proper objects too.

Here is a CSV file with data. Let’s use this file as an example,

Read CSV files c

 static void Main(string[] args)
        {
            CsvParserOptions csvParserOptions = new CsvParserOptions(true, ',');
            CsvUserDetailsMapping csvMapper = new CsvUserDetailsMapping();
            CsvParser<userdetails> csvParser = new CsvParser<userdetails>(csvParserOptions, csvMapper);
            var result = csvParser
                         .ReadFromFile(@"TestData.csv", Encoding.ASCII)
                         .ToList();
            Console.WriteLine("Name " + "ID   " + "City  " + "Country");
            foreach (var details in result)
            {
                Console.WriteLine(details.Result.Name + " " + details.Result.ID + " " + details.Result.City + " " + details.Result.Country);
            }
      }

In the above example below the line does all tricks,

var result = csvParser.ReadFromFile(@"TestData.csv", Encoding.ASCII).ToList();

The mapping class that needs to be created based on the CSV file’s Column and Row details is as below,

private class CsvUserDetailsMapping : CsvMapping<Userdetails>
        {
            public CsvUserDetailsMapping()
                : base()
            {
                MapProperty(0, x => x.ID);
                MapProperty(1, x => x.Name);
                MapProperty(2, x => x.City);
                MapProperty(3, x => x.Country);
            }
        }

A supporting model entity class UserDetails is as below,

 
public class UserDetails
    {
        public string ID { get; set; }
        public string Name { get; set; }
        public string City { get; set; }
        public string Country { get; set; }
    }

Here is the output on the console,

blank

TinyCSVParser is a very easy-use and efficient way of handling CSV files in C#.


Using CsvHelper Parser to Read CSV files – Approach 2

Csvhelper is one of the simple and lightweight most preferred parsers for CSV Reading and writing purposes.

Please see more details here,

Using TextFieldParser for CSV – Approach 3

TextFieldParser is also an easy and simple parser for reading CSV files.

You don’t really need to use any third-party software etc. as it comes with .NET Core 3.0 + above by default.

Please see more details here,

Do you have any comments or ideas or any better suggestions to share?

Please sound off your comments below.

Happy Coding !!

Summary

Today in this post we learned a few techniques for dealing with CSV files including reading or writing in .NET Core framework-based application.



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



4 thoughts on “Read CSV files in C# .NET Core with examples

Leave a Reply

Your email address will not be published. Required fields are marked *