Write LINQ to parse aspx page using HtmlAgilityPack

asp.net c# html-agility-pack linq

Question

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

Accepted Answer

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;

Popular Answer

I don't know if you've already found the answer for this, but here is the solution which worked.

HtmlAgilityPack.HtmlDocument doc = new HtmlDocument();
HtmlNode.ElementsFlags.Remove("form");
doc.LoadHtml(aspPage);
var elements = doc.DocumentNode.Descendants("div");  
var pageControls = from z in elements.ChildNodes
                     where z.Attributes.Contains("runat") //server controls
                     select z.Attributes["ID"].Value;



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why