The program is meant to read in information from a .csv
file; then with the data from this file the Product
objects are to be created and then stored in a list.
My problem is that I have no idea how to transfer the data from the .csv
file that will be split up by ',' and stored in an array to the constructor objects. Any help would be greatly appreciated.
The .csv
looks like this:
What the .csv Looks Like
Here is my code thus far:
class Product
{
public string ID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Price { get; set; }
public string StockAvailable { get; set; }
public string WeeklySales { get; set; }
// Constructor
public Product(string iD, string name, string Desc, string price, string StockAva, string weeklysales)
{
ID = iD;
Name = name;
Description = Desc;
Price = price;
StockAvailable = StockAva;
WeeklySales = weeklysales;
}
}
private static void ReadProductFile()
{
string productPath = GetDataDirectory("prod");
string[] fileData = File.ReadAllLines(productPath);
string[] productDetails = new string[20];
for (int i = 0; i < fileData.Length; i++)
{
productDetails = fileData[i].Split(',');
// I have no idea what do do next!
}
}
-
1What does the .csv file contain? Is it all of the constructor arguments? Can you give us an example .csv file?AJ Richardson– AJ Richardson2014年12月21日 17:09:01 +00:00Commented Dec 21, 2014 at 17:09
-
1you product constructor has 6 fields but your productdetails has an array of 20? I don't understand this?SuncoastOwner– SuncoastOwner2014年12月21日 17:12:31 +00:00Commented Dec 21, 2014 at 17:12
-
Sorry the .csv looks like this; tinypic.com/view.php?pic=xkrasi&s=8#.VJb_w_9_gJJRazis– Razis2014年12月21日 17:13:50 +00:00Commented Dec 21, 2014 at 17:13
2 Answers 2
List<Product> myProducts = new List<Product>();
...
for (int i = 0; i < fileData.Length; i++)
{
productDetails = fileData[i].Split(',');
var p = new Product(productDetails[0],
productDetails[1],
...
productDetails[5]));
myProducts.Add(p);
}
As others have mentioned, Split(',')
is not the most reliable way to parse a CSV file -- what if the product description contains a ,
? Using a proper C# CSV parser will fix these issues for you.
5 Comments
This is simple, but requires you to know beforehand the order of fields in your csv file. Once you know that, it is simply a matter of reading all specific fields and send them to the constructor of the Product
class (which fortunately already accepts the field values).
You should probably use a CSV reader for this task. That will make parsing and reading individual field values much easier. There is a built-in CSV parser within .NET class library. See this SO post for details and usage.
Your function would look something like below if you use CSV parser:
private static List<Product> ReadProductFile()
{
string productPath = GetDataDirectory("prod");
if(!System.IO.File.Exists(productPath)) return null;
var lst = new List<Product>();
TextFieldParser parser = new TextFieldParser(productPath);
parser.TextFieldType = FieldType.Delimited;
parser.SetDelimiters(",");
while (!parser.EndOfData)
{
string[] fields = parser.ReadFields();
foreach (string field in fields)
{
try
{
//assuming that the order of fields in the CSV file is the same as the
//order of arguments of Product's constructor.
Product p = new Product(field[0], field[1], ...);
lst.Add(p);
}
catch(Exception ee)
{
//Log error somewhere and continue with the rest of the CSV
}
}
}
parser.Close();
return lst;
}