HTML agility pack get all divs with class

18,978

You could use SelectNodes method

foreach(HtmlNode div in document.DocumentNode.SelectNodes("//div[contains(@class,'listevent')]"))
{
}

If you are more familiar with css style selectors, try fizzler and do this

document.DocumentNode.QuerySelectorAll("div.listevent"); 
Share:
18,978
Burak Gazi
Author by

Burak Gazi

Updated on June 11, 2022

Comments

  • Burak Gazi
    Burak Gazi almost 2 years

    I am trying to scape a complicated HTMl. I need to get some text from div's with certain class.

    What I am trying to do is have the html agility pack to go over the whole html and find all divs whos class contains "listevent" and return me those.

    When I searched online I found out that If I map it , it is possible, but some of these divs are under somemany divs so trying to find some easy way.

    The HTML looks like this

    <div>
        <div>
           <table>
              <tr>
                <td>
                  <div class="thisone listevent"></td>
                <td>
                  <div class="thisone listevent"></td>
               </tr>
             </table>
         </div>
     </div>
    
  • Burak Gazi
    Burak Gazi about 10 years
    I tried but it returns null. Unless I map excatly where are the divs( I mean like SelecNodes("//div/div/td/...") it turns all null
  • Burak Gazi
    Burak Gazi about 10 years
    Sorry , apparently I was having problem when loading the html.