I have to gather information from a product page which does not have any class or id. I am using htmlagilitypack and c# 4.0.
There are many tables at this product page source code. The prices table contains ” KDV” string. So i would like to get this ” KDV” string containing table. How can i do that ?
The xpath below would select all tables for example
string srxPathOfCategory = "//table";
var selectedNodes = myDoc.DocumentNode.SelectNodes(srxPathOfCategory);
The code below selects the table but starting from most outer table. I need to select most inner table which contains that given string
//table[contains(., ' KDV')]
c# , xpath , htmlagilitypack
Use:
This selects any
tablein the XML document that doesn’t have atabledescendant, and that has a text node descendant that contains the string" KDV".In general the above expression could select many such
tableelements.If you want only one of them selected (say the first), use this XPath expression — do notice the brackets:
Remember: If you want to select the first
someNameelement in the document, using this (as in the currently accepted answer) is wrong:This is the second most FAQ in XPath (after the one how to select elements with unprefixed names in an XML document with a default namespace).
The expression above actually selects any
someNameelement in the document, that is the first child of its parent — try it.The reason for this unintuitive behavior is because the XPath
[]operator has a higher precedence (priority) that the//pseudo-operator.The correct expression that really selects only the first
someNameelement (in any XML document), if such exists is:Here the brackets are used to explicitly override the default XPath operator precedence.