How to remove the <br> tag in my html string using HtmlAgilityPack in C#?

c# c#-4.0 html-agility-pack

Question

I have one HTML string and i am using HtmlAgilityPack for parsing html string.

This is my html string

<p class="Normal-P" style="direction: ltr; unicode-bidi: normal;"><span class="Normal-H">sample<br/></span> <span class="Normal-H">texting<br></span></p>

This HTML string has <br> tag in two places.So, i want to remove both the tags...

can you help me to remove all <br> tags in my html string...

Accepted Answer

It's as easy as:

  • loading the HTML fragment into an Agility Pack HtmlDocument
  • getting all <br /> tags using the "//br" xpath expression
  • removing the tags obtained at the previous step using the Remove() method
  • inspecting the result in the DocumentNode.OuterHtml property

Here it is in code:

const string htmlFragment =
    @"<p class=""Normal-P"" style=""direction: ltr; unicode-bidi: normal;"">" +
    @"<span class=""Normal-H"">sample<br/></span>" +
    @"<span class=""Normal-H"">texting<br></span></p> ";

var document = new HtmlAgilityPack.HtmlDocument();
document.LoadHtml(htmlFragment);

foreach (var brTag in document.DocumentNode.SelectNodes("//br"))
    brTag.Remove();

Console.WriteLine(document.DocumentNode.OuterHtml);

Popular Answer

string html = ...;
string html = Regex.Replace(html, "<br>", "", RegexOptions.Singleline);



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