VB.NET & HtmlAgilityPack parse element attributes

html-agility-pack parsing vb.net

Question

I have HTML file like this

<html>
      <head>

    <title>Page Name in a Folder</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <meta name="apple-mobile-web-app-capable" content="yes"/>
    <link href="resources/css/jquery-ui-themes.css" type="text/css" rel="stylesheet"/>
    <link href="resources/css/axure_rp_page.css" type="text/css" rel="stylesheet"/>
    <link href="data/styles.css" type="text/css" rel="stylesheet"/>
    <link href="files/page_name_in_a_folder/styles.css" type="text/css" rel="stylesheet"/>

  </head>
  <body>
    <div id="base" class="">

      <!-- Image Shape Name (Image) -->
      <div id="u0" class="ax_image" data-label="Image Shape Name">
        <img id="u0_img" class="img " src="images/page_name_not_in_a_folder/u0.png"/>
        <!-- Unnamed () -->
        <div id="u1" class="text">
          <p><span>&nbsp;</span></p>
        </div>
      </div>

      <!-- Heading 1 Shape Name (Shape) -->
      <div id="u2" class="ax_h1" data-label="Heading 1 Shape Name">
        <img id="u2_img" class="img " src="resources/images/transparent.gif"/>
        <!-- Unnamed () -->
        <div id="u3" class="text">
          <p><span>Heading 1</span></p>
        </div>
      </div>

      <!-- Heading 2 Shape Name (Shape) -->
      <div id="u4" class="ax_h2" data-label="Heading 2 Shape Name">
        <img id="u4_img" class="img " src="resources/images/transparent.gif"/>
        <!-- Unnamed () -->
        <div id="u5" class="text">
          <p><span>Heading 2</span></p>
        </div>
      </div>

      <!-- Label Shape Name (Shape) -->
      <div id="u6" class="ax_paragraph" data-label="Label Shape Name">
        <img id="u6_img" class="img " src="resources/images/transparent.gif"/>
        <!-- Unnamed () -->
        <div id="u7" class="text">
          <p><span>Label</span></p>
        </div>
      </div>



      <!-- Unnamed (HTML Button) -->
      <div id="u26" class="ax_html_button">
        <input id="u26_input" type="submit" value="Submit"/>
      </div>
    </div>
  </body>
</html>

And I need to extract all DIV's with its classes and attributes, eg:

  • Class name: (ax_html_button) extract value of the button = “Submit”
  • Class name: (ax_paragraph) extract value of the data-label = “Label Shape Name”

etc.

Tried with using HtmlAgilityPack:

Public Shared Sub parseAgility(fName As String)
        Dim htmlDoc As New HtmlAgilityPack.HtmlDocument()
        htmlDoc.OptionFixNestedTags = True

        htmlDoc.Load(fName)

        Dim classes As New List(Of String)()
        For Each node As HtmlNode In htmlDoc.DocumentNode.SelectNodes("//body//div")

            classes.Add(node.InnerHtml)
       Next
    End Sub

But not sure how to handle all the attributes. Any idea ?

and how to get value of input element ("Submit") ?

 <div id="u26" class="ax_html_button">
    <input id="u26_input" type="submit" value="Submit"/>
  </div>

if I type this I get value of "u16_input" element instead of "u26" !?

For Each node As HtmlNode In htmlDoc.DocumentNode.SelectNodes("//body//div")
            Dim className = node.GetAttributeValue("class", "")
            Select Case className
                Case "ax_html_button"
                    Dim node2 As HtmlNode = node.SelectSingleNode("//input")

                    value= node2.GetAttributeValue("value", "")
                Case "ax_paragraph"
                Case "ax_h1"
                Case "ax_h2"
                Case "ax_h3"
                Case "ax_h4"
                Case "ax_h5"
                Case "ax_h6"
                Case "ax_checkbox"
            End Select
        Next

EDIT: Found the solution.

Accepted Answer

It isn't entirely clear what you're trying to get. But here is an example to get class name from class attribute :

For Each node As HtmlNode In htmlDoc.DocumentNode.SelectNodes("//body//div")
    Dim className = node.GetAttributeValue("class", "");
    If Not String.IsNullOrEmpty(className) Then classes.Add(className)
Next

You can use similar approach to get other attributes of the <div>s.




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