Results 1 to 5 of 5

Thread: HtmlAgilityPack

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2010
    Posts
    57

    HtmlAgilityPack

    Im having a hard time finding tutorials for the HtmlAgilityPack, all of them are for c#, so im having to use c# code and convert it to vb.

    Here is the my code, im still getting errors with the 3rd line:
    Code:
            Dim doc As HtmlAgilityPack.HtmlDocument = New HtmlAgilityPack.HtmlDocument
            doc.Load("http://www.yellowpages.ca/search/si/1/Estheticians/Calgary+AB ")
     For each HtmlNode in link doc.DocumentNode.SelectNodes("@'//span[@class=''listingTitle'']")
                Dim content As String
                content = link.InnerText
            Next
    And here is the c# code that im converting from:
    Code:
    HtmlDocument doc = new HtmlDocument();
     doc.Load("http://www.yellowpages.ca/search/si/1/Estheticians/Calgary+AB ");
     foreach(HtmlNode link in doc.DocumentElement.SelectNodes(@"//span[@class=""listingTitle"]"))
     {
      string content = link.InnerText;
     }

  2. #2
    Frenzied Member
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,158

    Re: HtmlAgilityPack

    It must be like this

    For Each link as HtmlNode in doc.Docu....

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2010
    Posts
    57

    Re: HtmlAgilityPack

    Thanks, that seems to have worked. One last thing please, How can I set the retrieved text into a textbox on my form?

    Here is what I tried:
    Code:
    Dim content As String
    
            Dim doc As HtmlAgilityPack.HtmlDocument = New HtmlAgilityPack.HtmlDocument
    
            doc.Load("http://www.yellowpages.ca/search/si/1/Estheticians/Calgary+AB ")
    
            For Each link As HtmlNode In doc.DocumentNode.SelectNodes("@'//span[@class=''listingTitle'']")
    
                content = link.InnerText
    
            Next
    
            TextBox1.Text = content
    But im getting "Variable 'content' is used before it has been assigned a value, a null reference exception could result at runtime." Im guessing I need to do some kind of If statement, but im not sure how.

  4. #4
    Frenzied Member
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,158

    Re: HtmlAgilityPack

    No Issues,Thats only a Warning but not an Error

    you can even clear it by using this

    Dim content as String = ""

  5. #5

    Thread Starter
    Member
    Join Date
    Aug 2010
    Posts
    57

    Re: HtmlAgilityPack

    Ok, I fixed my above problem I believe, but now I have a new problem:
    Code:
    Dim content As String
        Dim doc As New HtmlDocument
        doc.LoadHtml("http://www.yellowpages.ca/search/si/1/Estheticians/Calgary+AB ")
        For Each link As HtmlNode In doc.DocumentNode.SelectNodes("@'//span[@class=''listingTitle'']")
          content = link.InnerText
          TextBox1.Text = content
        Next
    I replaced DocumentElement with DocumentNode in the 4th line because vb didnt understand DocumentElement. When the app runs this line it gives me an error:

    "XPath exception was unhandled.

    Expression must evaluate to a node-set."
    Last edited by Datadayne; Sep 6th, 2010 at 04:46 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width