我想找出为什么我的代码不起作用(或者,得到一段工作示例代码)
我想要做的是使用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)
不幸的是,我无法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要容易得多.
| 归档时间: |
|
| 查看次数: |
1297 次 |
| 最近记录: |