使用HTMLAgilitypack通过xpath选择ASPX节点的代码示例

im_*_*chc 2 html-agility-pack

我想找出为什么我的代码不起作用(或者,得到一段工作示例代码)

我想要做的是使用HTMLAgilityPack通过XPATH表达式查询ASPX文件

这是代码,如果你输入// asp:content,你有0个节点(为清楚起见,省略了Form1.designer.cs):

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);
            }

        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是测试aspx,请保存到文件,并输入到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>
Run Code Online (Sandbox Code Playgroud)

注意:

  1. 我知道添加命名空间http://www.w3.org/1999/xhtml/没有意义,请告知添加命名空间的正确方法
  2. 当输入是// tr,// td等时,它可以工作
  3. doc.DocumentNode.Descendants("asp:Content")有效,但我需要接受用户输入的XPath,因此不会考虑(对于LiNQ for XML也是如此)

Ale*_*lex 7

不幸的是,我无法XmlNamespaceManager正确使用HtmlAgilityPack.但是您可以使用xpath name()函数来查询asp:类似的节点.看到:

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

asp:content将从您的片段返回2个节点.

但是使用LINQ to XML要容易得多.

  • 看起来这确实是使用HtmlAgilityPack避免XML命名空间的最佳选择.小心!查询中的标记名称需要以小写形式提供,例如`asp:dropdownlist`,否则`SelectNodes`将出错. (2认同)