I need to extract all the paragraph from one html and also all text between that tags.
this code is not working when the text parsed into HtmlDocument get changed from the original one. In the sample
some <br />text
is changed in
some <br>text
es:
string s = "<p>firt paragraph</p>some <br />text<p>another paragraph</p><span>some text between span</span><p>hellow word</p>";
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(s);
var nodes = doc.DocumentNode.SelectNodes("//p");
int lastPos = -1;
foreach (HtmlAgilityPack.HtmlNode n in nodes)
{
if (lastPos > -1)
{
string textNotInP = Doc.DocumentNode.OuterHtml.Substring(lastPos, n.StreamPosition - lastPos);
System.Diagnostics.Debug.WriteLine(textNotInP);
}
System.Diagnostics.Debug.WriteLine(n.OuterHtml);
lastPos = n.StreamPosition + n.OuterHtml.Length;
}
the correct result would be:
<p>firt paragraph</p>
some <br>text
<p>second paragraph</p>
<span>some text between span</span>
<p>third paragraph</p>
but the code above return this:
<p>firt paragraph</p>
some <br>text<p
<p>second paragraph</p>
pan>some text between span</span><p
<p>third paragraph</p>
the reason is steamPosition return the node position related to the original text, not that one parserd in htmlDocument.
Is There a way to return the position of one node related to the parsed html?
OuterHtml
in the first place? – har07 May 19 '16 at 10:40firt paragraph
' (this is between opening tag and closing tag p) or 'some <br>text
' (this one is between two p elements)? which one do you mean by 'between the p tags'? – har07 May 19 '16 at 10:44