1 回答

TA貢獻1719條經驗 獲得超6個贊
您的結構錯誤,您不應在結構中添加列表,結構的每條記錄應僅包含一個國家/地區的條目,然后您可以像這樣創建結構列表:
public struct CSVline
{
public string Country;
public string Continent;
public int popualtion;
public int landmass;
public long Bignum;
public CSVline(string Country, string Continent, int popualtion, int landmass, long Bignum)
{
this.Country = Country;
this.Continent = Continent;
this.popualtion = popualtion;
this.landmass = landmass;
this.Bignum = Bignum;
}
}
那么你可以這樣做:
string line;
var csv = new List<CSVline>();
using (StreamReader sr = new StreamReader(@"filepath of csv"))
{
try
{
while ((line = sr.ReadLine()) != null)
{
var values = line.Split(',');
csv.Add(new CSVline(values[0],values[1],values[2],values[3],values[4]));
}
}
catch (Exception ex)
{
Console.WriteLine("The file could not be read {0}", ex);
}
}
- 1 回答
- 0 關注
- 235 瀏覽
添加回答
舉報