The third part export application we use will not properly render paragraph tags (does not include the extra line between paragraphs), so I am trying to replace all paragraph tags with two linebreak tags using HtmlAgilityPack.
Here is what I have so far…
// Shortened for this example
string rawHtml = "<p><strong><span>1.0 Purpose</span></strong></p><p><span>The role</span></p><p><span>NOTE: Defined...</span></p>";
HtmlDocument doc = new HtmlDocument();
HtmlNode.ElementsFlags["br"] = HtmlElementFlag.Empty;
doc.LoadHtml(rawHtml);
doc.OptionWriteEmptyNodes = true;
// Updated using suggestion from Petr
HtmlNode linebreak = doc.CreateElement("br");
var paragraphTags = doc.DocumentNode.SelectNodes("p");
for (int i = 0; i < paragraphTags.Count; i++)
{
HtmlNode childNode = HtmlNode.CreateNode(paragraphTags[i].InnerHtml);
HtmlNode nextNode = paragraphTags[i];
if (i > 0)
{
nextNode = doc.DocumentNode.InsertAfter(linebreak, nextNode);
nextNode = doc.DocumentNode.InsertAfter(linebreak, nextNode);
}
doc.DocumentNode.InsertAfter(childNode, nextNode);
paragraphTags[i].Remove();
}
It does remove the paragraph tag but only renders one line break. I have searched the internet to get as far as I have but nothing seems to work.
OuterHtml looks like this….
<strong><span>1.0 Purpose</span></strong><br /><span>The role</span><br /><span>NOTE: Defined...</span>
Any idea what I am doing wrong?
I feel like there HAS to be an easier way, is there?
Figured it out. Upvote to Petr and Simon for the suggestions. The key seemed to be that I needed two different linebreak nodes.