Answered Using HTML agility pack to select a node.

  • Thursday, January 10, 2013 10:08 AM
     
      Has Code

    Hi Guys,Ok I am using html agility pack.I am using the link below as the webpage reference. I was to return the total number of search matches (in this case 31) into my project.

    http://www.brokerforum.com/electronic-components-search-en.jsa?originalFullPartNumber=45678&x=77&y=9&hasNoSearchCriteria=false

    The html that I need to drill down to is as follows...

    <div class="resultsDescription">
       <p> Results <span>1-25</span> for 45678 (of <span>31</span>)</p>
    </div>

    My code so far is as follows.....

      HtmlWeb hw = new HtmlWeb();
                HtmlAgilityPack.HtmlDocument doc = hw.Load("http://www.brokerforum.com/electronic-components-search-en.jsa?originalFullPartNumber=45678&x=77&y=9&hasNoSearchCriteria=false");
                HtmlNode linkNodes = doc.DocumentNode.SelectSingleNode("//div[@class=\"resultsDescription\"]/span");
    
                txtDeleteAfter.Text = linkNodes.ToString();

    Many Thanks,

    Dean


All Replies

  • Thursday, January 10, 2013 11:04 AM
     
      Has Code

    Use two slashes before span

    "//div[@class=\"resultsDescription\"]//span"

    or add /p

    "//div[@class=\"resultsDescription\"]/p/span"

  • Thursday, January 10, 2013 11:16 AM
     
     

    Ok i did that and my textbox became populated with the following string.... "HtmlAgilityPack.HtmlNode"

  • Thursday, January 10, 2013 12:27 PM
     
      Has Code
    linkNodes.InnerText

  • Thursday, January 10, 2013 1:53 PM
     
      Has Code

    Getting closer, of the below.....

    <div class="resultsDescription">
       <p> Results <span>1-25</span> for 45678 (of <span>31</span>)</p>
    </div>

    The result im now getting is ......1-25

    How can I navigate to the 31?

  • Thursday, January 10, 2013 2:49 PM
     
     Answered Has Code

    You are using the SelectSingleNode method, that gives the one value.

    Instead, you should use the SelectNodes, that will give the collection. Then iterate.

    HtmlNodeCollection linkNodes = doc.DocumentNode.SelectNodes("//div[@class=\"resultsDescription\"]//span");
    
    foreach (var node in linkNodes)
        txtDeleteAfter.Text += node.InnerText;

     

    Sorry for my bad English.

    • Marked As Answer by deans.general Monday, January 14, 2013 1:37 PM
    •  
  • Thursday, January 10, 2013 3:57 PM
     
      Has Code

    Ok so does this mean it will set the textbox to

    1-25

    but then overwrite it with

    31

    as it iterates through?

    Your English is fine.