Html Agility Pack - New HtmlAttribute

谁都会走 提交于 2019-12-06 22:34:12

问题


Using Html Agility Pack in C# I have a node I'd like to add an attribute to.

Currently the node is an <li> element with no attributes and I'd like to add a class to it of "active".

It looks like the best thing to use would be node.Attributes.Add(attrClass)

Where attrClass is a HtmlAttribute of class="active".

However if I try to define a new HtmlAttribute I get an error stating that it doesn't have any constructors. Eg HtmlAttribute attrClass = new HtmlAttribute();

Is there something wrong with my Html Agility Pack reference, or am I doing something incorrectly?

Is there another method I could use to achieve my goal?


回答1:


node.Attributes.Add("class","active");



回答2:


The HtmlAttribute class has one constructor, which is internal. Therefore you'd not have access to actually call it, thus you'd get an error either way.

However, it is exposed elsewhere, under the HtmlDocument class.

So:

HtmlDocument document = new HtmlDocument();
var attribute = document.CreateAttribute("class", "active");

You then have a HtmlAttribute representing the class attribute with a value of active.



来源:https://stackoverflow.com/questions/18400619/html-agility-pack-new-htmlattribute

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