开发者

Http Agility Pack - Accessing Siblings?

开发者 https://www.devze.com 2023-01-21 23:56 出处:网络
Using the HTML Agility Pack is great for getting descendants and whole tables etc... but how can you use it in the below situation

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

...Html Code above...

<dl>
<dt>Location:</dt>
<dd>City, London</dd>
<dt style="padding-bottom:10px;">Distance:</dt>
开发者_如何学C<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....

How could you find If miles was less than 15 in this case, I undestand you could do something with elements but would you have to get all elements find the correct one and then find the number just to check its value? Or is there are way to use regex with Agility pack to achieve this in a better way...


I'm pretty sure (haven't checked) that it supports the following-sibling:: axis, so you could either find the node "dt[.='Distance:']" and then find node.SelectSingleNode("following-sibling::dd[1]") - or (simpler) just use node.NextSibling if you are sure that the dd always immediately follows the dt.

For example:

string distance = doc.DocumentNode.SelectSingleNode(
          "//dt[.='Distance:']/following-sibling::dd").InnerText;


Get just html siblings

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

精彩评论

暂无评论...
验证码 换一张
取 消