What is the best way to retrieve all sibling ul tags?

html-agility-pack

Question

The following HTML snippet: between: zzz-5 zzz

<h3 class="my-subject">a</h3>

<ul class='ex'>....</ul>
<ul class='ex'>....</ul>
<ul class='ex'>....</ul>
<ul class='ex'>....</ul>
<ul class='ex'>....</ul>
<ul class='ex'>....</ul>

<h3 class="my-subject">b</h3>

<ul class='ex'>....</ul>
<ul class='ex'>....</ul>
<ul class='ex'>....</ul>
<ul class='ex'>....</ul>
<ul class='ex'>....</ul>
<ul class='ex'>....</ul>

How do I just receive the six siblings?ul.ex via <h3 class="my-subject">a</h3>

Obtain the following sixul via <h3 class="my-subject">b</h3> ?

1
0
2/16/2012 9:14:49 AM

Accepted Answer

given the prevalence of<ul> Tags may change. Here are a few ideas you may consider.

Applying XPath

var h3Xpath = "//h3[@class='my-subject']";
var query = doc.DocumentNode.SelectNodes(h3Xpath);
foreach (var h3 in query)
{
    var value = h3.InnerText;
    var ulXpathFmt = "following-sibling::ul[@class='ex' and " +
        "preceding-sibling::h3[@class='my-subject'][1]='{0}']";
    var uls = h3.SelectNodes(String.Format(ulXpathFmt, value));
    // do something with the uls
}

With general, I wouldn't do it entirely using XPath but instead mix in some LINQ.

var h3Xpath = "//h3[@class='my-subject']";
var query = doc.DocumentNode.SelectNodes(h3Xpath);
foreach (var h3 in query)
{
    var ulXpath = "following-sibling::*";
    var uls = h3.SelectNodes(ulXpath)
        .TakeWhile(tag => tag.Name == "ul" &&
                          tag.Attributes["class"].Value == "ex");
    // do something with the uls
}
1
2/19/2012 7:07:38 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow