Even after having isolated to a child node via SelectNodes(), applying SelectSingleNode() to the child will search from the root above (presume from root, above anyway). This behaviour is unexpected, I expect it to search from the isolated child and below only.
Example:
<HTML>
<HEAD><TITLE> HTML Agility Bug Demo</TITLE></HEAD>
<BODY>
<somestuff>stuff here</somestuff>
<table>
<tr><td>first row</td></tr>
<tr><td>second row</td></tr>
<tr><td>third row</td></tr>
</table>
</BODY>
</HTML>
HtmlAgilityPack.HtmlDocument doc = new HtmlDocument();
doc.Load(@"HtmlAgilityBugDemo.html");
HtmlNodeCollection rowNodes = doc.DocumentNode.SelectNodes("//table/tr");
foreach (HtmlNode row in rowNodes)
{
string test1 = row.InnerText; // Works, enumerates correctly
string test2 = row.SelectSingleNode("//td").InnerText; // This ALWAYS returns "first row" !!
string test3 = row.SelectSingleNode("//somestuff").InnerText; // Found somestuff. But no stuff within this node !!
}
Comments: ** Comment from web user: kimotun **
the issue come from this function
private HtmlDocument LoadUrl(Uri uri, string method, WebProxy proxy, NetworkCredential creds)
{
HtmlDocument doc = new HtmlDocument();
doc.OptionAutoCloseOnEnd = false;
doc.OptionFixNestedTags = true;
_statusCode = Get(uri, method, null, doc, proxy, creds);
if (_statusCode == HttpStatusCode.NotModified)
{
// read cached encoding
doc.DetectEncodingAndLoad(GetCachePath(uri));
}
return doc;
}
just change the variable doc.OptionFixNestedTags = false;