Reputation: 1675
I want to search within all nodes and all attributes for a specific text using xpath query.
Example:
<Employee>
<Id>1</Id>
<Info FirstName="Search" LastName="Smith">
<Address City="Search"/>
</Info>
</Employee>
I am currently using the below xpath query:
var nodes = xmlConfig.SelectNodes("//*[contains(@*,'Search')]");
Xpath query should return Info node and Address node. However, it only returns one node.
Upvotes: 2
Views: 7348
Reputation: 89285
Assuming that you're using XPath 1.0, your XPath should have returned Info
node and Address
node as you expect. See online demo for that in the following link (click on 'test' button to execute the XPath) :
//*[contains(@*,'Search')]
In XPath 2.0, the above will throw exception passing sequence of more than one item is not allowed as the first argument of contains()
, and that happen because one element may have more than one attribute. For XPath 2.0 compatible expression, you can do this way instead :
//*[@*[contains(.,'Search')]]
output :
<Info FirstName="Search" LastName="Smith">
<Address City="Search"/>
</Info>
<Address City="Search"/>
Upvotes: 4