Skip to content Skip to sidebar Skip to footer

Select Divs And Put Into Collection Using Htmlagilitypack Not Working

Why does this not work? I get a null reference exception error on the foreach loop as it starts I'm trying to get all the divs text on a page and put each one into my own collectio

Solution 1:

Your code looks correct. Perhaps the URL "http://www.mysite.com" is not returning a valid HTML.

The code below works:

Imports HtmlAgilityPack

PublicClass _Default
    Inherits System.Web.UI.Page

    ProtectedSub Page_Load(ByVal sender AsObject, ByVal e As System.EventArgs) HandlesMe.Load
        Dim webGet As HtmlWeb = New HtmlWeb
        Dim htmlDoc As HtmlDocument = webGet.Load("http://stackoverflow.com/q/11528387/1350308")

        Dim ids AsNew List(OfString)()
        TextBox1.Text = ""ForEach div As HtmlNode In htmlDoc.DocumentNode.SelectNodes("//div")
            TextBox1.Text += div.Id + Environment.NewLine
        NextEndSubEndClass

The result in TextBox1 is:

noscript-padding
notify-container
overlay-header
custom-headerheader
portalLink
topbar
hlinks
hsearch

hlogo
hmenus


content

question-header
mainbar
question
adzerk1

















comments-11528387


answers
answers-header

tabs
answer-11528559








comments-11528559

post-editor

wmd-button-bar

draft-saved
draft-discarded
wmd-preview









sidebar
newuser-box



adzerk2
hireme



























feed-link
feed-link-text

prettify-lang
footerfooter-menufooter-sites
footer-flair
svnrev
copyright
noscript-warning

Complete source: Q11528387WebApp.7z

Post a Comment for "Select Divs And Put Into Collection Using Htmlagilitypack Not Working"