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:
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:
- 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).
- CSVColumn – Represents a CSV File Column and tracks thinks like the column and row number and the original data.
- CSVFile – This represents a CSV File itself with a collection of rows and a CSV File Reader (CSVReader) of buffered data.
- 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:
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!