HTMLAgilityPack gets innerText of td tag with id attribute

I am trying to select the inner text of td with id attribute using HTMLAgilityPack.

HTML code:

<td id="header1">    5    </td>
<td id="header2">    8:39pm    </td>
<td id="header3">    8:58pm    </td>
...

the code:

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);
    }
}

I keep getting null nodes because I am not choosing the td tag correctly but cannot understand what I did wrong ...

Edit:

I made a mistake with header1 and header2, but there are 5 different td tags with headers from 1 to 5.

+5
source share
3 answers

You are trying to select header1, but id header2.

You can also directly use GetElementById:

var td = doc.GetElementbyId("header2");
+6
source

.. , - . <td> id="header1". , , header1 header5, :

for (int i = 1; i <= 5; i++ ) {
    var tdNode = doc.DocumentNode.SelectSingleNode(string.Format("//td[@id='header{0}']", i));

    //do something with the node here
}

, , null, <td> (, - //tr[@id='some-id']//td[contains(@id, 'header')].

+1

You can solve your problem with InnerHtml Property Like:

var td = doc.GetElementbyId("header2").InnerHtml;
0
source

All Articles