Search code examples
c#xpathhtml-agility-packselectsinglenode

Why is HtmlAgilityPack SelectSingleNode not finding divs


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?


Solution

  • 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);