HTMLNode from Line/Position number in HTML Agility Pack

c# html-agility-pack winforms

Question

I'm trying to find the corresponding HtmlNode for a given Line/Character position (Eg. Line 5, character 12). I've looked around the help documentation but I'm not too sure it's available.

Is this possible to do in the Html Agility Pack?

EDIT:

Sample HTML file:

<!DOCTYPE html>
<html>
<body>

<h4>An Ordered List:</h4>
<ol>
  <li>Coffee</li>
  <li>Tea</li>
  <li>Milk</li>
</ol>

</body>
</html>

I'm trying to get position line 7, character 5 -> Coffee LI and 12th Line, get back node.

Popular Answer

If you are talking about the HTML at line 5, position 12, you could do something like this:

private void button1_Click_1(object sender, EventArgs e)
{
    HtmlAgilityPack.HtmlWeb web = new HtmlAgilityPack.HtmlWeb();
    HtmlAgilityPack.HtmlDocument doc;
    doc =  web.Load("http://slashdot.org");


    var node = CheckLine(doc.DocumentNode);
    if (node != null)
        MessageBox.Show(node.OuterHtml);
}

private HtmlAgilityPack.HtmlNode CheckLine(HtmlAgilityPack.HtmlNode node)
{
    if (node.Line == 5 && node.LinePosition < 12 && ((node.LinePosition + node.OuterHtml.Length) > 12))
        return node;

    foreach (var n in node.ChildNodes)
    {
        var val = CheckLine(n);
        if (val != null)
            return val;
    }
    return null;
}



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