1 回答

TA貢獻(xiàn)1719條經(jīng)驗(yàn) 獲得超6個(gè)贊
您的結(jié)構(gòu)錯(cuò)誤,您不應(yīng)在結(jié)構(gòu)中添加列表,結(jié)構(gòu)的每條記錄應(yīng)僅包含一個(gè)國(guó)家/地區(qū)的條目,然后您可以像這樣創(chuàng)建結(jié)構(gòu)列表:
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 關(guān)注
- 215 瀏覽
添加回答
舉報(bào)