HTMLAgilityPack get innerText of a td tag with an id attribute

社会主义新天地 提交于 2019-12-01 16:27:04

问题


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

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 getting null nodes because I am not selecting the td tag correctly but cannot figure out what I have done wrong...

Edit:

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


回答1:


You are trying to select header1 but the id is header2.

You could also use GetElementById directly:

var td = doc.GetElementbyId("header2");



回答2:


Hmm.. I don't think you're doing anything wrong. Your code should give you only the <td> with id="header1". If you have, let's say, from header1 to header5, you can do:

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
}

although I suggest you posting your entire code, so that we can tell you why you're getting null, and also a better way of parsing the <td> nodes without doing the above loop (eg. something like //tr[@id='some-id']//td[contains(@id, 'header')].




回答3:


You can Solve your Problem by using the InnerHtml Property Like:

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


来源:https://stackoverflow.com/questions/15448772/htmlagilitypack-get-innertext-of-a-td-tag-with-an-id-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!