Processing CSV File

Posted by nettguy on Stack Overflow See other posts from Stack Overflow or by nettguy
Published on 2010-04-11T15:01:04Z Indexed on 2010/04/11 15:03 UTC
Read the original article Hit count: 283

Filed under:
|
|

I am using Sebastien LorionReference CSV reader to process my CSV file in C# 3.0.

Say example

id|name|dob (Header)
1|sss|19700101 (data)
2|xx|19700201  (data)

My Business Object is

class Employee
{
   public string ID {get;set;}
   public string Name {get;set;}
   public string Dob {get;set;}
}

I read the CSV stream and stored it in List<string[]>

List<string[]> col = new List<string[]>();

using (CsvReader csv = new CsvReader
               (new StreamReader("D:\\sample.txt"), true, '|'))
{
    col = csv.ToList();
}

How to iterate over the list to get each Employee like

     foreach (var q in col)
    {
        foreach (var r in q)
        {
            Employee emp=new Employee();
            emp.ID =r[0];
            emp.Name=r[1];
            emp.Dob=r[2];
        }
    }

If i call r[0],r[1],r[2] i am getting "index out of range exception".How the process the list to avoid the error?

© Stack Overflow or respective owner

Related posts about c#

Related posts about csv