HTMLAgilityPack get innerText of a td tag with an id attribute

泪湿孤枕 提交于 2019-12-01 17:31:26

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

You could also use GetElementById directly:

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

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')].

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

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