-4

I want to remove background-color attribute from string (html) example :

<p style=\"background-color:#eeeeee\">Hellow world</p>

will be

<p >Hellow world</p>  

or

<p style=\"\">Hellow world</p>

in c#

Omar Seleim
  • 21
  • 1
  • 6

1 Answers1

1

You could remove the style-attribute with the XmlDocument class. For a whole page, it will be the challenge to find the right nodes to do so. (Maybe run though child nodes recursively..) But here an example of the string you posted - to remove the style:

    static void Main(string[] args)
    {
        XmlDocument xml = new XmlDocument();
        xml.LoadXml("<p style=\"background-color:#eeeeee\">Hellow world</p>");
        var attributesofFirst = xml.ChildNodes[0].Attributes;
        attributesofFirst.RemoveNamedItem("style");
        Console.WriteLine(xml.ChildNodes[0].OuterXml); //<p>Hellow world</p>  
        Console.ReadLine();
    }
Malior
  • 1,221
  • 8
  • 16