Converting CSV of unknown size to custom Object with C#

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.

-------------Problems Reply------------

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.

Ok, this is a little off the wall but it could work.

You could load your "line" array up into a stack of strings, and pop each item off the stack as you assign them to the fields. This assumes, of course, that if any items are "missing" then they're missing from the end of the line.

So here's the idea:

var fields = new Stack<string>(line);
Person.Name = fields.PopOrDefault();
Person.Age = fields.PopOrDefault();
Person.Height = fields.PopOrDefault();

I'm using a "PopOrDefault" extension method because obviously the "Pop" method on Stack<T> will throw an exception if there are no more items. Here's the implementation for that (it's pretty straight-forward):

static class StackExtensions
public static T PopOrDefault<T>(this Stack<T> stack)
if (stack.Count == 0) return default(T);
return stack.Pop();

So if any fields are missing, the property will get the default value for that type (in this case the default for string, which is null). You could even add a second parameter to "PopOrDefault" so you could specify your own default value.

You might also take a look at the open source library for reading CSV files (and other file types). It should handle missing fields and you can specify optional fields.

How many versions of the csv file are there? This seems like a simple answer, but could you do something like:

bool isOlderVersion = string.length == OLD_VERSION_NUM_COLUMNS;

Person.Name = isOlderVersion ? line[0] : line[0];
Person.Age = isOlderVersion ? line[1] : line[1];
Person.Height = isOlderVersion ? 0 : line[2];
Person.Width = isOlderVersion ? line[2] : line[3];

This isn't very efficient and if you have 40 columns will be hard to read but the concept would definitely work.

Category:c# Views:0 Time:2009-11-06
Tags: c# csv

Related post

  • Converting from String to custom Object for Spring MVC form Data binding? 2009-05-26

    I am using Spring MVC's SimpleFormController in conjunction with Spring MVC's form JTL to create a form to edit a Generic object. On my form I have a drop down where the user can specify a server via a drop down. <form:form commandName="generic"

  • Convert Custom Object To DataRowView C# WinForms 2009-11-17

    I have a custom object as follows public partial class _AccessionType { private string accessionIdField; private string docUrlField; /// <remarks/> public string AccessionId { get { return this.accessionIdField; } set { this.accessionIdField =

  • Converting array of ObjectProxy objects to custom objects 2011-06-10

    I have a service which returns an Array of ObjectProxy objects. I would like to cast this to a custom object (a value object) and create an ArrayCollection. How can I do this? Edited: I am using Django and PyAMF for the backend. I had to write a cust

  • iPhone app has a "Shopping Cart" -How to convert custom objects into XML to be sent to the server 2011-06-12

    I have a shopping cart in my iPhone app. Once the user is done with his shopping, I would like to send the contents of the cart to a server where a PHP script processes the input. After researching online, I saw that I could convert all the contents

  • How to convert Custom Objects to NSString in Objective C? 2011-12-08

    I am using Objective C Proxy objects(which was created using wsdl2Objc tool) to access SOAP Based web services. It is a .Net based Web service. It returns XML data. The C# based Proxy Objects for accessing that web service is working properly. @inter

  • Converting json to custom object array 2011-12-17

    I am trying to create a reddit app (just for practice) for Windows Phone 7 using C# and Json.Net. I am trying to get a hang of converting json to something c# can work with, along with some other things. I can pull in the data I want, but don't know

  • Converting NSMutableArray to Array of custom objects in sudzc 2012-03-21

    I am using SUDZC to get data from a web service. The generated code looks like this: // Do something with the NSMutableArray* result NSMutableArray* result = (NSMutableArray*)value; I can see that there is a response when I called the web service but

  • In Java, what is the best way to determine the size of an object? 2008-09-09

    For example, let's say I have an application that can read in a CSV file with piles of data rows. I give the user a summary of the number of rows based on types of data, but I want to make sure that I don't read in too many rows of data and cause Out

  • converting CSV/XLS to JSON? 2009-03-19

    Does anyone know if there is application that will let me covert preferably XLS to JSON? I'll also settle for a converter from CSV since that's what I'll probably end up having to write myself if there is nothing around. --------------Solutions------

  • BlazeDS and ArrayList of custom objects 2009-04-16

    I'm using BlazeDS to connect Flex with Java. I'm having trouble passing ArrayLists of custom objects from Flex to java. I have two objects, one is called Category, the other Section. A Category has an ArrayList of Section objects. I can send an Array

  • Iterate through Custom Object's Property Names and Values 2009-09-01

    I'm trying to create an export Excel/CSV function that will iterate through a custom object and first output the property names and then output the values. I want to use reflection only where necessary so I'm attempting to save the property names whe

  • Convert .csv to .xls in Java 2010-07-06

    Does anyone here know of any quick, clean way to convert csv files to xls or xlsx files in java? I have something to manage csv files already in place and I need the extra compatibility for other programs. Sample code in addition to package names is

  • Is there a way to convert tables of text into a PowerShell Object 2011-04-21

    There are many tools that output their data in in a table format. One such example is diskpart. Shaving off some extraneous output, you would get something like this. Disk ### Status Size Free Dyn Gpt -------- ------------- ------- ------- --- --- Di

  • Write custom object to .plist in Cocoa 2011-07-04

    I am blocking into something and I am sure it is too big. I have a custom object that look like this @interface DownloadObject : NSObject <NSCoding>{ NSNumber *key; NSString *name; NSNumber *progress; NSNumber *progressBytes; NSNumber *size; NS

  • Salesforce: Moving custom object with data to contact after lead to contact conversion? 2011-09-13

    I have some leads which have a custom browsing activity object which stores the links they browse through. When I convert a lead to a contact, everything except the custom object gets transferred over. Is there a way to import that custom object with

  • asp.Net GridView bind custom object with nested List 2008-11-17

    I have a List of custom object, which consist of a custom list. class person{ string name; int age; List<friend> allMyFriends; } class friend{ string name; string address; } I'trying to bind a list of these objects to a GridView and the Grid sh

  • How do I make a pointer to a multidimensional array which has an unknown size? 2009-02-09

    how do I make a pointer to a multidimensional array, which have a unknown size? I've tried this: int **triangles; triangles = new int[numTriangles][3]; But i get this error: cannot convert 'int (*)[3]' to 'int**' in assignment --------------Solutions

  • Storing custom objects in an NSMutableArray in NSUserDefaults 2009-02-11

    I've recently been trying to store the search results of my iPhone app in the NSUserDefaults collection. I also use this to save user registration info successfully, but for some reason trying to store my NSMutableArray of custom Location classes alw

  • Persisting Custom Objects 2009-03-15

    I have a custom object that simply inherits from NSObject. It has 3 members - two floats and an NSDate. My app is going to have an array with a number of these objects kicking around, and I need to persist it between runs. How can I accomplish this?

Copyright (C), All Rights Reserved.

processed in 0.075 (s). 11 q(s)