1
votes

On the real world C# script I tried every permutation (although hopefully someone will tell me I have missed one) that I understand to be viable for the XPath value, but always returning null. Striped it back to what would seem to be the simplest use of SelectSingleNode but still for some reason it will only return a value with <body>

Doc assigned thus:

HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();

This is the first time I have looked into XPath and I have had limited experience with HtmlAgilityPack. So it's no doubt a very basic failing on my part. However:

HtmlAgilityPack.HtmlNode node = doc.DocumentNode.SelectSingleNode("//body");

result: node is not null

replacing it with div or any other element (that ARE in the script-tested more than one)

HtmlAgilityPack.HtmlNode node = doc.DocumentNode.SelectSingleNode("//div");

Result:

node is null

What have I missed?

1
Your syntax for finding a div tag is correct. IF the result is null, that means there are no div tags in the html document. - broke
Yes indeed, however as I said these tags are in the script, and it is returning null. So what might cause it to fail? Example script that I used to the code code can be found on adventurebods.com/home/ - Andrew Seabrook
In fact your correct broke hence adding an answer to this myself. - Andrew Seabrook

1 Answers

0
votes

Sorry my bad - got to the bottom of this. Whilst the example at the adventurebods URL above was the source of the intended test script, it transpires in fact I have something less. The process preceding this requirement copies from the clipboard the script - and of course being a lemon I had assumed this to be correct but in fact I have a truncated script after using:

htmlText = Clipboard.GetText(TextDataFormat.Html);