Http Agility Pack - Access to Siblings?

Using the HTML Agility Pack is great for getting descendants and whole tables, etc., but how can you use it in the situation below.

...Html Code above...

<dl>
<dt>Location:</dt>
<dd>City, London</dd>
<dt style="padding-bottom:10px;">Distance:</dt>
<dd style="padding-bottom:10px;">0 miles</dd>
<dt>Date Issued:</dt>
<dd>26/10/2010</dd>
<dt>type:</dt>
<dd>cement</dd>
</dl>

...HTML Code below....

As you could find, if there were less than 15 in this case, I realized that you can do something with the elements, but you will need to find all the elements to find the correct one, and then find the number to check its value ? Or is there a way to use regex with the Agility package to achieve this better ...

+3
source share
2 answers

( ), following-sibling::, node "dt[.='Distance:']", node.SelectSingleNode("following-sibling::dd[1]") - () node.NextSibling , dd dt.

:

string distance = doc.DocumentNode.SelectSingleNode(
          "//dt[.='Distance:']/following-sibling::dd").InnerText;
+3
Get just html simblings



public static List<HtmlNode> GetHtmlNodeList(string html)
    {
        HtmlDocument doc = new HtmlDocument();
        doc.LoadHtml(html);
        var regs = doc.DocumentNode.SelectSingleNode("//div");
        var first = regs.Descendants().FirstOrDefault();
        var second = first.NextSibling;
        List<HtmlNode> list = new List<HtmlNode>();
        while (second != null)
        {
            list.Add(second);
            second = CheckSibling(second);
        }
        return list;
    }
    private static HtmlNode CheckSibling(HtmlNode node)
    {
        node = node.NextSibling;
        return node;          
    }
0

Source: https://habr.com/ru/post/1771670/


All Articles