Sto cercando di selezionare il testo interno di un td con un attributo id con HTMLAgilityPack.
Codice html:
<td id="header1"> 5 </td>
<td id="header2"> 8:39pm </td>
<td id="header3"> 8:58pm </td>
...
Codice:
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(data);
var nodes = doc.DocumentNode.SelectNodes("//td[@id='header1']");
if (nodes != null)
{
foreach (HtmlAgilityPack.HtmlNode node in nodes)
{
MessageBox.Show(node.InnerText);
}
}
Continuo a ricevere nodi null perché non sto selezionando correttamente il tag td ma non riesco a capire cosa ho fatto di sbagliato ...
Modificare:
Ho fatto un errore con header1 e header2, ma ci sono 5 diversi tag di td con le intestazioni da 1 a 5.
Stai provando a selezionare header1
ma l'id è header2
.
Puoi anche utilizzare direttamente GetElementById
:
var td = doc.GetElementbyId("header2");