解析外部html文件時出現對象引用錯誤,我想這是因爲並非所有選中的元素都有類名。這裏是我的代碼:從具有特定類名的元素中選擇值
foreach (HtmlNode link in doc.DocumentNode.Descendants("li").Where(i => i.Attributes["class"].Value == "name"))
{
string result = link.InnerText.Trim().Replace(" ", "");
Console.WriteLine(result);
}
如何只選擇我的班級名稱爲「名稱」的值?
這裏是我的html代碼我試圖解析:
<li>
<span class="name">
<a href="/players/joe-bloggs.html">Joe, Bloggs</a>
</span>
<span class="country">
<img src="/img/flags/15x15/USA.gif" alt="USA"/>
United States
</span>
</li>
<li>
<span class="name">
<a href="/players/joe-bloggs.html">Joe, Bloggs</a>
</span>
<span class="country">
<img src="/img/flags/15x15/USA.gif" alt="USA"/>
United States
</span>
</li>
<li>
<span class="name">
<a href="/players/joe-bloggs.html">Joe, Bloggs</a>
</span>
<span class="country">
<img src="/img/flags/15x15/RSA.gif" alt="RSA"/>
South Africa
</span>
</li>
你能告訴你所得到的錯誤? – BenM