"Expression must evaluate to a node-set." error while using XPath

.net c# html-agility-pack node-set xpath


I'm having trouble retrieving a single node by its explicit XPath that I have already found by other ways. I have node and I can get its XPath, but when I try to retrieve that same node again this time via node.XPath it gives the "expression must evaluate to a node-set" error. Shouldn't this work? I'm using HtmlAgilityPack in C# btw for the HtmlDocument.

HtmlDocument doc = new HtmlDocument();
HtmlNode node = doc.DocumentNode.SelectSingleNode("(//node()[@id='something')])[first()]");
HtmlNode same = doc.DocumentNode.SelectSingleNode(node.XPath);

BTW: this is the value of node.XPath:

8/13/2010 1:35:08 PM

Accepted Answer

I was able to get it working by replacing #text with the function text(). I'm not sure why it didn't just emit the XPath that way in the first place.

HtmlNode same = doc.DocumentNode.SelectSingleNode(node.XPath.Replace("#text","text()");
8/15/2010 3:30:32 AM

Popular Answer

Your XPath ends in "#text[2]", which means "the second 'text' attribute". Attributes aren't nodes, they're node metadata.
This is a common problem I've had with XPath: wanting the value of an attribute while the XPath operation absolutely has to extract a node.

The solution I've used for this is to wrap my XPath fetching with something that detects and strips off the attribute portion of the string (via a myXPathString.LastIndexOf( "#" ) method call) and then uses the truncated myXPathString to fetch the node and collect the desired attribute value as a second step.

Hope that helps,

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow