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>
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.