HtmlAgilityPack ArgumentOutOfRangeException最后一行

时间:2013-09-15 21:20:11

标签: c# asp.net html-agility-pack

我无法打开以下页面。最后一行问题。我不想要排在底线。我该怎么办。

HtmlWeb web = new HtmlWeb();
web.OverrideEncoding = Encoding.GetEncoding("windows-1254");
HtmlAgilityPack.HtmlDocument doc = web.Load("http://www.yerelnet.org.tr/ilceler/ilce.php?ilceid=198191");

var ilceNufus =  doc.DocumentNode
.SelectNodes("//*[@id='Table_01']/tr[2]/td[1]/table/tr/td/table[5]/tr/td[2]/table[1]/tr/td[3]/table[2]/tr")
.Skip(1)
.Select(td => td.Elements("td").Select(row => row.InnerText).ToList())
.Select(td => new { Yil = td[0], Toplam = td[1], Sehir = td[2], Koy = td[3] })
.ToList();

dataGridView1.DataSource = ilceNufus;

1 个答案:

答案 0 :(得分:0)

只需添加Where

即可
var ilceNufus = doc.DocumentNode.SelectNodes("//*[@id='Table_01']/tr[2]/td[1]/table/tr/td/table[5]/tr/td[2]/table[1]/tr/td[3]/table[2]/tr")
                .Skip(1)
                .Select(td => td.Elements("td").Select(row => row.InnerText).ToList())
                .Where(tds => tds.Count == 4)
                .Select(td => new { Yil = td[0], Toplam = td[1], Sehir = td[2], Koy = td[3] }).ToList();