user123456789
user123456789

Reputation: 2004

Remove html markup from string

I am reading in a field from the database and displaying in a GridView and in that field it contains <br/> tags in the text. So I am trying to remove these from the code but when I check the value of e.Row.Cells[index].Text it doesn't contain <br/> and is has ;br/&gt; instead.

So I tried creating a function that removes any substring starting with < and ending with > or starting with & and ending with ;. The code removes the <> but it is still showing br/

Code:

index = gv.Columns.HeaderIndex("Message");
if (index > 0)
{
   string message = RemoveHTMLMarkup(e.Row.Cells[index].Text);
   e.Row.Cells[index].Text = message;
}

static string RemoveHTMLMarkup(string text)
{
        return Regex.Replace(Regex.Replace(text, "<.+?>", string.Empty), "&.+?;", string.Empty);
}

How do I remove the <br/> tag?

Upvotes: 0

Views: 1515

Answers (3)

Karr
Karr

Reputation: 484

Being regardless of the question, I am curious about what this is:

enter image description here

Upvotes: 1

Thomas Ayoub
Thomas Ayoub

Reputation: 29431

Since this is a literal string, you (sh|c)ould only use String.Replace():

static string RemoveHTMLNewLines(string text)
{
    return text.Replace("&lt;br/&gt;", string.Empty);
}

Or replace with Environment.NewLine if needed.

Upvotes: 4

ravindra
ravindra

Reputation: 357

  1. De-entitize the string.
  2. Then use regex to to find and remove expected tags.

Or

If you have enough time to study and Use, then use HtmlAgilityPack package.

About HtmlAgilityPack

Nuget Package Link

Upvotes: 2

Related Questions