Html Agility Pack - New HtmlAttribute

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

Currently, node is an <li> element with no attributes, and I would like to add an "active" class to it.

It seems like it is best to use node.Attributes.Add(attrClass)

Where attrClass is HtmlAttribute of class="active" .

However, if I try to define a new HtmlAttribute , I get an error indicating that it has no constructors. For example, HtmlAttribute attrClass = new HtmlAttribute();

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

Is there any other way that I could use to achieve my goal?

+6
source share
2 answers
 node.Attributes.Add("class","active"); 
+16
source

The HtmlAttribute class has one constructor, which is internal . Therefore, you would not have access to actually call it, this way you will get an error anyway.

However, it appears elsewhere in the HtmlDocument class.

So:

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

Then you have an HtmlAttribute representing the class attribute with the value active .

+5
source

Source: https://habr.com/ru/post/952352/


All Articles