html nested tables agility pack valid xpath

html html-agility-pack xpath

Question

Assuming nested tables don't have unique attributes ( id , class or anything else ) to get the required one via

doc.DocumentNode.SelectSingleNode("//table[@width='500']")

Does XPath prohibit using table several times in its path ?

foreach (HtmlNode table in doc.DocumentNode.SelectNodes("//table/tr/center/table"))

throws exception as SelectNodes returns null.

If so how to tackle parsing of html with nested tables without specific attributes with Agility Pack ?

Accepted Answer

I just missed "td" tag in my path. So

foreach (HtmlNode table in doc.DocumentNode.SelectNodes("//table/tr/td/center/table/tr/td/center/table"))

does work. That actually answers my question. Other workaround to get the same table assuming there are unique attribute values in parent elements could be

HtmlNode tbl = doc.DocumentNode.SelectSingleNode("//td[@height='643']/center/table");



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why