Gene S
Gene S

Reputation: 2793

HtmlAgilityPack replace paragraph tags with linebreaks

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?

Upvotes: 1

Views: 4395

Answers (2)

Gene S
Gene S

Reputation: 2793

Figured it out. Upvote to Petr and Simon for the suggestions. The key seemed to be that I needed two different linebreak nodes.

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;

HtmlNode linebreak1 = doc.CreateElement("br");
HtmlNode linebreak2 = doc.CreateElement("br");
var paragraphTags = doc.DocumentNode.SelectNodes("p");
for (int i = 0; i < paragraphTags.Count; i++)
{
    if (i > 0)
    {
        doc.DocumentNode.InsertBefore(linebreak1, paragraphTags[i]);
        doc.DocumentNode.InsertBefore(linebreak2, paragraphTags[i]);
    }
    doc.DocumentNode.InsertBefore(HtmlNode.CreateNode(paragraphTags[i].InnerHtml), paragraphTags[i]);
    paragraphTags[i].ParentNode.RemoveChild(paragraphTags[i]);
}

Upvotes: 6

pabdulin
pabdulin

Reputation: 35255

Does it help if you use

HtmlNode linebreak = doc.CreateElement("br");

to create linebreak node?

Upvotes: 2

Related Questions