从字符串中的C#去除背景色(闭合)

问题描述 投票:-4回答:1

我想删除从字符串(HTML)的背景色属性例如:

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

将会

<p >Hellow world</p>  

要么

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

在C#

c# regex
1个回答
1
投票

你可以删除样式属性与XmlDocument类。对于整个页面,它会找到合适的节点做这样的挑战。 (虽然也许子节点运行递归..)但在这里您发布的字符串的一个例子 - 删除样式:

    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();
    }
© www.soinside.com 2019 - 2024. All rights reserved.