Code sample using HTMLAgilitypack to select ASPX nodes by xpath

html-agility-pack

Question

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()
        {
            InitializeComponent();
            lbl_ErrMsg.Text = "";
        }

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

                doc.Load(txt_FilePath.Text);

                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",  "http://www.w3.org/1999/xhtml/"); // "http://tempuri.org/foo");
                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>
        <asp:content id="Content2" contentplaceholderid="MainContent" runat="server">
         <table>

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

               </table> 
        </asp:content>

Note:

  1. I know that adding the namespace http://www.w3.org/1999/xhtml/ 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)

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.



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why