【发布时间】:2012-01-19 19:49:00
【问题描述】:
我将如何有效地从中解析 href 属性值:
<tr>
<td rowspan="1" colspan="1">7</td>
<td rowspan="1" colspan="1">
<a class="undMe" href="/ice/player.htm?id=8475179" rel="skaterLinkData" shape="rect">D. Kulikov</a>
</td>
<td rowspan="1" colspan="1">D</td>
<td rowspan="1" colspan="1">0</td>
<td rowspan="1" colspan="1">0</td>
<td rowspan="1" colspan="1">0</td>
[...]
我有兴趣拥有玩家 ID,即:8475179这是我目前拥有的代码:
// Iterate all rows (players)
for (int i = 1; i < rows.Count; ++i)
{
HtmlNodeCollection cols = rows[i].SelectNodes(".//td");
// new player
Dim_Player player = new Dim_Player();
// Iterate all columns in this row
for (int j = 1; j < 6; ++j)
{
switch (j) {
case 1: player.Name = cols[j].InnerText;
player.Player_id = Int32.Parse(/* this is where I want to parse the href value */);
break;
case 2: player.Position = cols[j].InnerText; break;
case 3: stats.Goals = Int32.Parse(cols[j].InnerText); break;
case 4: stats.Assists = Int32.Parse(cols[j].InnerText); break;
case 5: stats.Points = Int32.Parse(cols[j].InnerText); break;
}
}
【问题讨论】:
-
如果
switch中有硬编码索引,为什么要使用for循环?为什么不player.Position = cols[2].InnerText; -
好点。我正在回收我写的一些旧代码,所以我没有想到。
标签: c# asp.net-mvc-3 html-parsing html-agility-pack