C# HTML Parsing using XPath - Namespace

asp.net-mvc-4 c# html-agility-pack xpath

Question

I'm trying to parse a HTML page using the HTML Agility Pack. I used a Firefox extension named XPath Checker and I'm rather sure that the expression is correct. However when I run my code, .NET tells me that "Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function."

That is fine except I have no idea where to get the Xslt or said Namespace from. How can I figure out what Namespace I need?

For reference, this is my code (I use MVC4):

List<Post> posts = new List<Post>();
            // Use this to count how many nodes to get
            int postNodesAmount = doc.DocumentNode.SelectNodes("//div[@class=\"post_block no_sidebar\" ]").Count;            

            for (int i = 1; i <= postNodesAmount; i++)
            {
                Post newPost = new Post();
                string newContent = doc.DocumentNode.SelectSingleNode("id('pane_forums:posts')/x:div/x:div[" + i + "]/x:div/x:div/x:div/x:p[1]").InnerText;
                newPost.Content = newContent;
                posts.Add(newPost);
            }

            return View(posts);

Accepted Answer

The Html Agility Pack XPATH implementation simply doesn't support queries involving namespaces.

The library is open source, so it can be changed, but this needs quite a bunch of rewrites to add this function.



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