Wednesday 18 November 2015

C# - Easiest CSV parser built in .NET Framework

Today it came to my surprise to know that there is a built in CSV parser in the .NET Framework library. Throughout the years, I have written many CSV parsers myself; but if I knew about this hidden parser, I might not have written any by myself.
Its hidden inside "Microsoft.VisualBasic" namespace.

Below is a sample usage of the class and methods to read a file "Person.csv".
In the sample, the line commenting character is "#"; the delimiting character is ",".
The class exposes property to configure delimiting character (comma in our sample) and comment token (# in our sample).
Any field which are enclosed within double quotes will escape delimiting characters and double quotes within the data field. It beautifully handles with by just setting the HasFieldsEnclosedInQuotes property to true.

using Microsoft.VisualBasic.FileIO;

var path = @"C:\Person.csv"; // Habeeb, "Dubai Media City, Dubai"
using (TextFieldParser csvParser = new TextFieldParser(path))
{
 csvParser.CommentTokens = new string[] { "#" };
 csvParser.SetDelimiters(new string[] { "," });
 csvParser.HasFieldsEnclosedInQuotes = true;

 // Skip the row with the column names
 csvParser.ReadLine();

 while (!csvParser.EndOfData)
 {
  // Read current line fields, pointer moves to the next line.
  string[] fields = csvParser.ReadFields();
  string Name = fields[0];
  string Address = fields[1];
 }
}

The parser has many more options. It can read a complete line as a single string. It can even read the remaining file content as a single string as well if needed in any case.

I will definitely rely on this parser rather than writing my own again.