C# – CSV File Reader


 

CSV is a comma separated values file, which allows data to be saved in a table structured format. CSVs look like a garden-variety spreadsheet but with a .csv extension (Traditionally they take the form of a text file containing information separated by commas, hence the name).

 

At work, when taking on a new client and integrating them into our Distribution systems, we mostly write import routines to take in data and process it into our back office file system. We mainly receive these as flat CSV files – because these are easy to work with. Lets face it, the better way would be to expose some public API endpoints and provide these to clients to push data in a temporary data store (then for us to post-process it into our back office). We don’t. In an ideal world we would, but we live in a world of ideals which involve providing lots of different flat files and writing some custom code each and every time, mainly because we don’t have the time to write a more modular and reusable application….until now.

I will explore in my own time, how best to create a CSV Reader system using C# and how to process a file and validate it. Because there is no defacto standard, I have chosen to work with RFC4180 for the default behaviour.  I have just started this and the 1st version was pushed to github on Monday and can be found here:

 

http://garfbradaz.github.io/bradaz.utils/

 

Snip

 

 

 

 

 

I’m not going to explain all the code now as I would like to try a video tutorial at explaining each individual class, but the gist of it is in Bradaz.Utils.IO namespace includes:

 

  1. CSVRow – Represents a CSV File Row and tracks things like the row number and the original data. Also stores a collection of CSV Columns (CSVColumn).
  2. CSVColumn – Represents a CSV File Column and tracks thinks like the column and row number and the original data.
  3. CSVFile – This represents a CSV File itself with a collection of rows and a CSV File Reader (CSVReader) of buffered data.image
    The project itself contains some example code on how to load a test file, validate it (simple validation currently) and display the contents. Currently I only load into a string to view it:

http://pastebin.com/embed_iframe.php?i=Sj9A4MM2

 

I next want to enchance the Parser based on a XML configuration file that will allow a user to specify things like how many columns should be in the file and what types to expect in those columns and convert to a usable object, no more strings! (Things like dates and integers). More to come!

One thought on “C# – CSV File Reader

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s