我有以下HTML:
<tbody>
<tr>
<td class="metadata_name">Headquarters</td>
<td class="metadata_content">Princeton New Jersey, United States</td>
</tr>
<tr>
<td class="metadata_name">Industry</td>
<td class="metadata_content"><ul><li><a href="/q-Engineering-Software-jobs.html" rel="nofollow">Engineering Software</a></li><li><a href="/q-Software-Development-&-Design-jobs.html" rel="nofollow">Software Development & Design</a></li><li><a href="/q-Software-jobs.html" rel="nofollow">Software</a></li><li><a href="/q-Custom-Software-&-Technical-Consulting-jobs.html" rel="nofollow">Custom Software & Technical Consulting</a></li></ul></td>
</tr>
<tr>
<td class="metadata_name">Revenue</td>
<td class="metadata_content">$17.5 Million</td>
</tr>
<tr>
<td class="metadata_name">Employees</td>
<td class="metadata_content">201 to 500</td>
</tr>
<tr>
<td class="metadata_name">Links</td>
<td class="metadata_content"><ul><li><a href="/url?q=http%3A%2F%2Fwww.site.com&h=085df2ca" target="_blank">Company website</a></li></ul></td>
</tr>
</tbody>
我希望能够将metadata_content值(例如“ $ 17.5百万”)加载到其中metadata_name = =的变量(例如:“ Revenue”)中。
我尝试使用这样的代码组合几个小时了...
orgHtml.DocumentNode.SelectNodes("//td[@class='metadata_name']")[0].InnerHtml;
但是我没有找到正确的组合。如果您有有用的SelectNodes语法-这将为我提供解决方案,我将不胜感激。
最佳答案
看来您要寻找的是这样的:
var found = orgHtml.DocumentNode.SelectSingleNode(
"//tr[td[@class = 'metadata_name'] = 'Revenue']/td[@class = 'metadata_content']");
if (found != null)
{
string html = found.InnerHtml;
// use html
}
请注意,要获取元素的文本,应使用
found.InnerText
而不是found.InnerHtml
,除非您特别需要其HTML内容。关于c# - HtmlAgilityPack SelectNodes语法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27506725/