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

强颜欢笑 提交于 2019-12-20 06:16:51

问题


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");

Is there a way to loop through all element tags ?


回答1:


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


来源:https://stackoverflow.com/questions/57119863/add-css-class-to-all-tags-in-tagbuilder-edit-existing-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!