0

Here is my html code:

<p><span style="background:lime;Color:Red;">Contrary to popular belief, <b><u>Lorem Ipsum is not simply</u></b> random text. It has roots in a piece of classical Latin literature from <span style="background:blue;">45 BC, making it over 2000 years</span> old. Richard McClintock, </span><b> 

From above code, i need to remove the background attributes & value using C# from all the spans. The other values in style tag should remain. Eg:

<span style="background:lime;Color:Red;">Contrary to popular belief,.....</span> 

should look

<span style="Color:Red;">Contrary to popular belief,.....</span> 

Pls help...!

4
  • 3
    Check out the HTML Agility Pack from Nuget. Commented Apr 22, 2013 at 13:42
  • You could do this easily in jQuery, why does it need to be C#? Commented Apr 22, 2013 at 13:42
  • How do you access this HTML? Commented Apr 22, 2013 at 13:43
  • I get this html using richtext control. see this thread..infragistics.com/community/forums/p/79418/400897.aspx#400897 Commented Apr 23, 2013 at 1:48

2 Answers 2

3

Using HtmlAgilityPack

string html = @"<span style=""background:lime;Color:Red;"">Contrary to popular belief,.....</span>"; var doc = new HtmlAgilityPack.HtmlDocument(); doc.LoadHtml(html); foreach (var span in doc.DocumentNode.Descendants("span")) { var style = span.Attributes["style"].Value; span.Attributes["style"].Value = String.Join(";", style.Split(';').Where(s => !s.ToLower().Trim().StartsWith("background:"))); } var newHtml = doc.DocumentNode.InnerHtml; 
Sign up to request clarification or add additional context in comments.

Comments

-2

Try this

$('span').css("background", "") 

1 Comment

From question: ...using c#

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.