Add CSS Class to All Tags in TagBuilder, Edit Existing Attribute

前端 未结 1 949
情歌与酒
情歌与酒 2021-01-28 17:59

How do I go through all the Tags in a Tagbuilder, both All Outer and Inner Elements, and Append \"test\" to their CSS class .

tagbuilder.AddCssClass(\"test\");
<         


        
1条回答
  •  长发绾君心
    2021-01-28 18:36

    One of the best package for parsing HTML is HtmlAgilityPack.

    Using this package you can easily select necessary nodes and add class attribute.

    It can be done like in example below:

    var htmlDoc = new HtmlDocument();
    htmlDoc.LoadHtml(html);
    var node = htmlDoc.DocumentNode.SelectNodes("//input[contains(@class, 'example')]").FirstOrDefault();
    node.Attributes.Add("class","test");
    

    For more details please review next StackOverflow topics:

    • TagBuilder Find Specific Inner Element and Add New Attribute
    • Html Agility Pack - New HtmlAttribute

    0 讨论(0)
提交回复
热议问题