写LINQ使用HtmlAgilityPack解析aspx页面 [英] Write LINQ to parse aspx page using HtmlAgilityPack

查看:114
本文介绍了写LINQ使用HtmlAgilityPack解析aspx页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看了看所以下面和类似的链接,并使用谷歌解析aspx页面HTMLAgilityPack

解析HTML文档使用 HtmlAgilityPack

但我不知道怎么写LINQ的语句,我可以在我的aspx页面识别按钮和标签控件名称。

下面是我的aspx页面。

 <%@页面语言=C#AutoEventWireup =真codeBehind =WebForm4.aspx.cs继承=WebApplication1.WebForm4%GT;!< D​​OCTYPE HTML PUBLIC -  // W3C // DTD XHTML 1.0过渡// ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">< HTML的xmlns =htt​​p://www.w3.org/1999/xhtml>
<头=服务器>
    <标题>< /标题>
< /头>
<身体GT;
    <表ID =form1的=服务器>
    < D​​IV>            < ASP:按钮的ID =Button1的=服务器文本=在第4页按钮/>
        < BR />
        < BR />
        < ASP:标签ID =Label1的=服务器文本=标签4页的>< / ASP:标签>
        < BR />
                    < BR />
        < ASP:按钮的ID =Button2的=服务器文本=第二个按钮第4页/>                        < BR />
        < ASP:按钮的ID =按钮3=服务器文本=第二个按钮第4页/>    < / DIV>
    < /表及GT;
< /身体GT;
< / HTML>

我想用HTML敏捷性包,这样我可以列出以下输出写LINQ:

此页面上的控件Button1的,标签1,Button2的,将Button3

我无法解析的aspx页面写LINQ。请帮忙。

这是我迄今书面和它不工作。

  HtmlAgilityPack.HtmlDocument HTMLDOC =新HtmlAgilityPack.HtmlDocument();    htmlDoc.OptionFixNestedTags = TRUE;    字符串文件路径= @C:\\ WebApplication1 \\ webform4.aspx
    htmlDoc.Load(文件路径);        htmlDoc.Load(文件路径);
        在htmlDoc.DocumentNode.Descendants从链接VAR pagecontrols =(格)
                           其中,links.Attributes.Contains(RUNAT)
                           选择links.Attributes [ID]值。        的foreach(在pagecontrols VAR的PageControl)
        {
            的Response.Write(的PageControl);
        }


解决方案

如果我正确理解你的问题,你需要做的是这样的:

  VAR pagecontrols =从htmlDoc.DocumentNode.Descendants链接(格)
                   其中,links.Attributes.Contains(RUNAT)
                   选择links.Attributes [ID]值。

I have looked at the following and similar links on SO and google to parse aspx page using HTMLAgilityPack

Parse html document using HtmlAgilityPack

But I don't know how to write LINQ statement such that I could identify Button and Label Control Names in my aspx page.

Here is my aspx page.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm4.aspx.cs" Inherits="WebApplication1.WebForm4" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

            <asp:Button ID="Button1" runat="server" Text="Button on page4" />
        <br />
        <br />
        <asp:Label ID="Label1" runat="server" Text="Label on page 4"></asp:Label>
        <br />
                    <br />
        <asp:Button ID="Button2" runat="server" Text="second button page 4" />

                        <br />
        <asp:Button ID="Button3" runat="server" Text="second button page 4" />



    </div>
    </form>
</body>
</html>

I want to write LINQ using HTML Agility pack such that I could list the following output:

Controls on this page are Button1, Label1, Button2, Button3

I am having trouble writing LINQ for parsing the aspx page. Please help.

Here is what I have written so far and its not working.

   HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();

    htmlDoc.OptionFixNestedTags = true;

    string filePath = @"C:\WebApplication1\webform4.aspx";


    htmlDoc.Load(filePath);

        htmlDoc.Load(filePath);


        var pagecontrols = from links in htmlDoc.DocumentNode.Descendants("div")
                           where links.Attributes.Contains("runat")
                           select links.Attributes["ID"].Value;

        foreach (var pagecontrol in pagecontrols)
        {
            Response.Write(pagecontrol);
        }

解决方案

If I'm understanding your problem correctly, you need to do something like this:

var pagecontrols = from links in htmlDoc.DocumentNode.Descendants("div")
                   where links.Attributes.Contains("runat")
                   select links.Attributes["ID"].Value;

这篇关于写LINQ使用HtmlAgilityPack解析aspx页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆