J'ai besoin d'extraire la valeur d'un seul td spécifique de la table en utilisant XPath, mais le code retourne toujours null. Comment puis-je réparer cela?
var location = GetLocation(document.Result.DocumentNode.SelectSingleNode("//*[@id='detailTabTable']/tbody/tr[3]/td[2]"));
et le code
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);
}
}
Et petite table simple:
<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>
Je viens de tester votre code. Comme mentionné dans les commentaires, lorsque vous supprimez tbody
de votre expression xpath, tout fonctionne correctement. CA marchait bien pour moi.
private static void htmlAgilityPackTest()
{
string html = " <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>";
HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument();
document.LoadHtml(html);
var node = document.DocumentNode.SelectSingleNode("//*[@id='detailTabTable']/tr[3]/td[2]");
string location = GetLocation(node);
Console.WriteLine("Location: " + location);
}
Au cas où je comprendrais quelque chose, s'il vous plaît faites le moi savoir.