2017-05-16 82 views
0

我试图通过使用Html Agility Pack在我的Html字符串内找到不同Html标签的开始/结束位置。如何使用Html Agility Pack找到html标签节点位置

样本HTML字符串:

This is a <a href="https://en.wikipedia.org/wiki/Health">custom</a> made html string that will serve as an example for the <a href="http://stackoverflow.com">StackOverflow</a> question described above.

成功运行,我需要得到2个阵列从一个标签的开始索引值如下代码:

int[] startIndex = new int[] { 11, 124 }; 
int[] endIndex = new int[] { 68, 176 }; 

其中11和125是标记开始的索引位置其中a标记,68和175表示相同标记的最后索引位置。

我知道,使用HTML敏捷包HtmlNode我可以得到LinePosition价值,这将使我开始指数与元素的innerHtml.Lenght沿着我可以计算出HTML元素的结束索引位置。

我能够通过使用来算一个元素:

int aNodesCount = htmlDoc.DocumentNode.SelectNodes("//a").Count; 

现在我需要通过他们都itereate并得到每个人的LinePosition值。这是我发现自己卡住的地方。

回答

-1

嗯,这是非常简单的,所以我将发布一个答案为自己得到别人同样的问题:

foreach (HtmlNode aNode in htmlDoc.DocumentNode.SelectNodes("//a")) 
{ 
    startIndex.Add(aNode.LinePosition); 
    endIndex.Add(aNode.LinePosition + aNode.OuterHtml.Length); 
} 
相关问题