HTMLAgilityPack get innerText of a td tag with an id attribute

c# html html-agility-pack html-table nodes

Question

Using the HTMLAgilityPack, I'm attempting to pick the inner text of a td that has an id property.

The HTML code

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

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 choosing the td tag incorrectly, which results in null nodes, but I can't tell out what I did wrong.

Edit:

There are five separate td tags with headers 1 to 5, however I made a mistake with header1 and header2.

1
6
3/16/2013 11:57:44 AM

Popular Answer

You are attempting to chooseheader1 though the id isheader2 .

Additionally, utilizeGetElementById directly:

var td = doc.GetElementbyId("header2");
6
3/16/2013 11:38:11 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow