Code sample using HTMLAgilitypack to select ASPX nodes by xpath



I would like to find out why my code doesn't work (or, get a piece of working sample code)

What I'm trying to do is to query an ASPX file using the HTMLAgilityPack, by an XPATH expression

This is the code, if you input //asp:content , you got 0 nodes (Form1.designer.cs omitted for clarity):

using hap = HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Xml.Linq;
using System.Text;
using System.Windows.Forms;

namespace hap_shell
    public partial class Form1 : Form
        public Form1()
            lbl_ErrMsg.Text = "";

        private void button1_Click(object sender, EventArgs e)
                lbl_ErrMsg.Text = "";
                hap.HtmlDocument doc = new hap.HtmlDocument();
                hap.HtmlWeb hw = new hap.HtmlWeb();


                var q = doc.DocumentNode.Descendants("asp:Content");
                var cnt = q.Count();
                var nodes = q.ToList();
                var nav = doc.CreateNavigator();
                System.Xml.XmlNamespaceManager mgr = new System.Xml.XmlNamespaceManager(nav.NameTable);
                mgr.AddNamespace("asp",  ""); // "");
                var selNodes =nav.Select(txt_xpath.Text, mgr);

                //var selNodes = doc.DocumentNode.SelectNodes(txt_xpath.Text);

                if (selNodes == null)
                    lbl_ErrMsg.Text = "No nodes match your query.";
                lbl_ErrMsg.Text = selNodes.Count.ToString() + " nodes selected";
            catch (Exception ex)
                lbl_ErrMsg.Text = (ex.Message);


this is the testing aspx, please save to a file, and input its full path to txt_FilePath:

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="back_cal.aspx.cs" Inherits="EDP_SYS.back_cal" %>
    <asp:content id="Content1" contentplaceholderid="HeadContent" runat="server" >
        <asp:content id="Content2" contentplaceholderid="MainContent" runat="server">

                <asp:button id="Button1" runat="server" text="Search"></asp:button></td></tr>



  1. I know that adding the namespace doesn't make sense, please advise the correct way of adding the namespace
  2. It works when input is //tr, //td etc
  3. doc.DocumentNode.Descendants("asp:Content") works, but I need to accept a user-input XPath, so it won't be considered (same for LiNQ for XML)
11/27/2012 2:20:53 AM

Accepted Answer

Unfortunately, I wasn't be able to make XmlNamespaceManager work with HtmlAgilityPack properly. But you can use xpath name() function to query asp:-like nodes. See:

var contentNodes = doc.DocumentNode.SelectNodes("//*[name()='asp:content']");

This will return 2 asp:content nodes from your fragment.

But using of LINQ to XML is much easier for this.

11/27/2012 1:35:05 PM

Related Questions


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