fetching ul li items under div class using html agility pack

.net c# html html-agility-pack

Question

<div class="outer">
    <div class="divOne"></div>
    <div class="divContent">
       <h3>SomeTitle</h3>
       <h4>SomeSubtitle</h4>
       <ul>
          <li><a href="/someUrlx.htm">SomeUrl</a>
               <span> Nr of records under this url </span>
          </li>
       </ul>
       <h4>Some Other Subtitle</h4>
       <ul>
          <li><a href="/someUrlx.htm">SomeUrl</a>
              <span> Nr of records under this url </span>
          </li>
       </ul>
     </div>
</div>

Once more, I want to fetch all unordered list items under above html structure

I'm able to fetch divContent class content using

var regs = htmlDoc.DocumentNode.SelectSingleNode(@"//div[@class='outer']");

var descendant = regs.Descendants()
                    .Where(x => x.Name == "div" && x.Attributes["class"].Value == "divContent")
                    .Select(x => x.OuterHtml);

now I need expression to fetch ul li items.

Accepted Answer

This should work fine:

IEnumerable<string> listItemHtml = htmlDoc.DocumentNode.SelectNodes(
    @"//div[@class='outer']/div[@class='divContent']/ul/li")
    .Select(li => li.OuterHtml);

Example: https://dotnetfiddle.net/fnDPLB


Update based on comments below:

If you want to find only <li> elements belonging to <ul> elements that are direct siblings of an <h4> element with the value "SomeSubtitle", here's an XPath expression that should work:

//div[@class='outer']      // Get div.outer
/div[@class='divContent']  // under that div, find div.divContent
/h4[text()='SomeSubtitle'] // under div.divContent, find an h4 with the value 'SomeSubtitle'
/following::ul[1]/li       // Get the first ul following the h4 and then get its li elements.

Example: https://dotnetfiddle.net/AfinpV




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