Note that there are some explanatory texts on larger screens.

plurals
  1. POCode sample using HTMLAgilitypack to select ASPX nodes by xpath
    primarykey
    data
    text
    <p>I would like to find out why my code doesn't work (or, get a piece of <strong>working</strong> sample code) </p> <p>What I'm trying to do is to query an ASPX file using the HTMLAgilityPack, by an XPATH expression</p> <p>This is the code, if you input <strong>//asp:content</strong> , you got 0 nodes (Form1.designer.cs omitted for clarity):</p> <pre><code>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); } } } } </code></pre> <p>this is the testing aspx, please save to a file, and input its full path to txt_FilePath:</p> <pre><code>&lt;%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="back_cal.aspx.cs" Inherits="EDP_SYS.back_cal" %&gt; &lt;asp:content id="Content1" contentplaceholderid="HeadContent" runat="server" &gt; &lt;/asp:content&gt; &lt;asp:content id="Content2" contentplaceholderid="MainContent" runat="server"&gt; &lt;table&gt; &lt;tr&gt;&lt;td&gt; &lt;asp:button id="Button1" runat="server" text="Search"&gt;&lt;/asp:button&gt;&lt;/td&gt;&lt;/tr&gt; &lt;/table&gt; &lt;/asp:content&gt; </code></pre> <h2>Note:</h2> <ol> <li>I know that adding the namespace <a href="http://www.w3.org/1999/xhtml/" rel="nofollow">http://www.w3.org/1999/xhtml/</a> doesn't make sense, please advise the correct way of adding the namespace</li> <li>It works when input is //tr, //td etc</li> <li>doc.DocumentNode.Descendants("asp:Content") works, but I need to accept a user-input <strong>XPath</strong>, so it won't be considered (same for LiNQ for XML)</li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload