I am trying to import a file in from my clients custom software. The file is basically a csv file with some custom escape characters in it. I am reading in the file line by line, then splitting each line into a string[]. I am then assigning each each element to a field in my custom object. For example:
Person.Name = line[0];
Person.Age = line[1];
Person.Height = line[2];
etc. The problem is, that some of the files I am importing are from an older version of the app and do not contain all the fields. So this line
Person.Height = line[2];
errors out because line.Length = 2 instead of 3.
Is there a “clean” way of solving this issue? I’ve gotten around it now by writing an if statement before each assignment to make sure the line[x] is valid, but that just seems kludgy to me.
I’ve got a couple suggestions, assuming you aren’t parsing huge CSVs where every drop of performance counts.
One approach I use is to have a step before you actually start working with the data where you ensure that the file is consistently formatted. In your case, that would mean scanning each row to determine if all the columns are present and if not, inserting default values for the missing data.
This can help keep your “cleanup” code separate from your data processing. This actually takes a little more coding and would be slower from a performance perspective (since you’re essentially parsing the file twice), but could help make your code a little easier to read and debug since you are breaking it into two separate activities.
An alternative would be to use a third-party library like LINQtoCSV to take care of marking “nullable” columns for you. Then you can refer to columns as named properties instead of by index.