我需要使用XPath從表中抽取一個特定td的值,但代碼始終返回null。我怎樣才能解決這個問題?HtmlAgilityPack找不到特定的td
var location = GetLocation(document.Result.DocumentNode.SelectSingleNode("//*[@id='detailTabTable']/tbody/tr[3]/td[2]"));
和代碼
private string GetLocation(HtmlNode h)
{
try
{
string location = null;
if (h == null)
{
location = "N/A";
}
else
{
location = h.InnerText;
location = location.Substring(0, location.IndexOf(",", StringComparison.InvariantCulture));
}
return location;
}
catch (Exception ex)
{
log.ErrorFormat("Error in Link Data Repository {0} in Parse Links {1}", ex.Message, ex.StackTrace);
throw new Exception(ex.Message);
}
}
而且簡單的小表:
<table id="detailTabTable" width="99%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td class="detailTabContentLt">Current List Price:</td>
<td class="detailTabContentPriceRt">
<span class="aiDetailCurrentPrice">AED 6,600,000</span>
</td>
</tr>
<tr>
<td class="detailTabContentLt" style="white-space: nowrap;">Plot size (Sq. Ft.):</td>
<td class="detailTabContentRt">N/A</td>
</tr>
<tr>
<td class="detailTabContentLt" valign="top">Locality</td>
<td class="detailTabContentRt">Dubai, Dubai</td>
</tr>
<tr>
<td colspan="2"></td>
</tr>
</table>
您的示例HTML沒有在XPath中提到的tbody ... – 2015-02-24 06:51:27
這個Xpath我是從Chrome開發者工具中自動獲得的。但是你是對的,我應該嘗試沒有這個標籤 – 2015-02-24 06:55:19
如果你從chrome得到這個XPath查詢,必須有另一個html代碼,而不是你告訴我們的。請更新您的html代碼,以便我們能夠幫助您! – 2015-02-24 06:56:54