views:

98

answers:

1

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();
doc.Load(@"..\..\test1.htm");
HtmlNode node = doc.DocumentNode.SelectSingleNode("(//node()[@id='something')])[first()]");
HtmlNode same = doc.DocumentNode.SelectSingleNode(node.XPath);

BTW: this is the value of node.XPath:

"/html[1]/body[1]/table[1]/tr[1]/td[1]/div[1]/div[1]/div[2]/table[1]/tr[1]/td[1]/div[1]/div[1]/table[1]/tr[1]/td[1]/div[1]/div[1]/div[4]/div[2]/div[1]/div[1]/div[4]/#text[2]"
A: 

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()");
Snives