This project has moved and is read-only. For the latest updates, please go here.

Error When Running "SelectNodes"

Topics: User Forum
May 2, 2010 at 10:35 PM

First off, here's the code I'm having trouble with.

            HtmlAgilityPack.HtmlDocument html = new HtmlAgilityPack.HtmlDocument();
            string url = urlBox.Text;

            html.LoadHtml("" + url + "&bwm=i&bwmo=d&bwmf=s");
            HtmlNodeCollection links = html.DocumentNode.SelectNodes("id('result-list')/x:tbody/x:tr/x:td[2]/a[1]");
            for (int i = 0; i < links.Count(); i++)
                MessageBox.Show("Found: " + links[i]);

Okay, now on line 4, I'm getting the following error when this code runs.

"Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function."

I've Googled this, but the trouble is that I'm finding things for the XML Namespace, when I'm using the HTML Agility Pack - which doesn't have an argument for a namespace in the selectnodes method.

So... I'm thinking that my SelectNodes is actually running from System.Xml - but I don't know how to change that.

Any idea on how to fix this would be greatly appreciated.

May 6, 2010 at 9:43 AM


"x:" references a namespace, and the Html Agility Pack does not support namespaces.

You can search this forum for more information on namespaces.

May 6, 2010 at 4:40 PM

Hmm... I've removed the "x:" but am still getting a null value.

I've changed the code to work on a site of my own, but even then I'm still having issues.

I know for a fact there are "p" tags on my site... so this *should* be showing it, I think.

Here's my modified code.

            HtmlAgilityPack.HtmlDocument html = new HtmlAgilityPack.HtmlDocument();
            //string url = urlBox.Text;
            string url = "test";

            HtmlNodeCollection links = html.DocumentNode.SelectNodes("p");

            if (links == null)
                MessageBox.Show("None Available");
                for (int i = 0; i < links.Count(); i++)
                    MessageBox.Show("Found: " + links[i]);

If my understanding of XPath is correct, simply placing the "p" in SelectNodes should pull out all paragraph tags. Now, I know the site is up, working, and it's straight HTML so I know there's no code to block LoadHtml from working correctly.

Any ideas on what the issue is?

May 6, 2010 at 4:41 PM

I've also tried using "//p" in SelectNodes and am still getting null.

Jul 8, 2010 at 8:37 AM

I am too having the same problem!

Let us request the author to provide option to add namespace while searching nodes