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 关注
- 120 浏览
添加回答
举报