source download : http://csv2entity.codeplex.com
What is CSV
CSV is theabbreviation of Comma-separated values, a file format stores tabular data in plain-textform, usually with .csv extension. CSV is a simple file format that is widely supported by consumer, business,and scientific applications.
Examples
Example of a USA/UK CSV file
Year,Make,Model,Length 1997,Ford,E350,2.34 2000,Mercury,Cougar,2.38
CSV Entity
In order to build CSV entities, we needsomething more. Csharp’s Attribute Mechanism provides us what we need by decorateCSV class properties with a custom attribute. Custom attribute defines CVSfield’s display name and order. Here is the CsvFieldAttribute:
public class CsvFieldAttribute:Attribute
{
public string Name { get; set; }
public int Order { get; set; }
}
With CsvFieldAttribute, now we can decorate our own class.
public class Car
{
[CsvField(Name = "Year", Order = 0)]
public string Year { get; set; }
[CsvField(Name = "Make", Order = 1)]
public string Make { get; set; }
[CsvField(Name = "Model", Order = 2)]
public string Model { get; set; }
[CsvField(Name = "Length", Order = 3)]
public string Length { get; set; }
}
GenerateCSV Class
So, do we need to create CSV classes manually?Of cause not. So I developed the following tool to generate CSV classes:
CSVReader
At last, we need a way to read CSV filesand put data into the CSV classes. The method is: split CSV line to an array,iterate over that array get index of each field, and reflect the CSV class (i.e.Cars above) get order of each property. When index of field and order ofproperty matches each other, transmit the value.
Following is theresult from Visual Studio 11 watch window.
References
CSV in Wikipedia: http://en.wikipedia.org/wiki/Comma-separated_values
source download : http://csv2entity.codeplex.com