HTMLAgilityPack获取带有id属性的td标签的innerText [英] HTMLAgilityPack get innerText of a td tag with an id attribute

查看:187
本文介绍了HTMLAgilityPack获取带有id属性的td标签的innerText的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用HTMLAgilityPack选择具有id属性的td的内部文本.

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

HTML代码:

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

代码:

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

我一直得到空节点,因为我没有正确选择td标签,但无法弄清楚我做错了什么...

I keep getting null nodes because I am not selecting the td tag correctly but cannot figure out what I have done wrong...

我在header1和header2上弄错了,但是有5个不同的td标签,它们的标题为1到5.

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

推荐答案

您正在尝试选择header1,但ID为header2.

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

您也可以直接使用GetElementById:

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

这篇关于HTMLAgilityPack获取带有id属性的td标签的innerText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆