When using HTML Agility Pack to query HTML for ID, an exception occurs.

html html-agility-pack parsing xpath

Question

I'm parsing an ASPX file inside of Visual Studio using the HTML Agility pack.

I'm looking for a certain element with the ID property.

I'm using this code:

var html = new HtmlAgilityPack.HtmlDocument();
html.LoadHtml(docText);
if (html.DocumentNode != null)
{
     try
     {
          var tagsWithId = html.DocumentNode.SelectNodes(string.Format("//[@id='{0}']", selector.Id));

It throws the error "Expression must evaluate to a node-set" when I execute this code, however.

Why does this "must" evaluate to a node-set, can somebody explain? Why can't it just return zero nodes (tagWithId.Count is called in the following line)? Surely 0 nodes can be found in the HtmlNodeCollection that the SelectNodes function returns?

Or is an incorrect Xpath expression to blame for the error? [The selector ID I'm using to verify this exists in the code without a doubt as div id="thisId">.]

I'm constructing an add-in; is it even feasible to import an ASPX file directly from Visual Studio? If not, would this include XML problems, and I'll have to load the output HTML stream instead? (i.e without the page declaration at the start of the file, etc.) ?

1
3
7/4/2010 12:48:20 PM

Accepted Answer

The argument being made has a flaw.SelectNodes() :

//[@id='{0}']

Zzz-16-Zzz (after doing the replacement). Therefore, the issue is not because the XPath expression "returns no nodes" is problematic; rather, the issue is that it is syntactically incorrect.

According to the W3C XPath Spec:

"// it stands for/descendant-or-self::node()/ "

The enlarged version of what was said above is as follows:

/descendant-or-self::node()/[@id='{0}']

Keep in mind that the last placement step begins with the predicate and lacks a node-test. The syntax guidelines for XPath state that this is forbidden.

Most likely, you desire:

//*[@id='{0}']
5
7/4/2010 3:35:30 PM


Related Questions





Related

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