Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Html Agility Pack - New HtmlAttribute

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?

like image 767
Tom Bowen Avatar asked Aug 23 '13 10:08

Tom Bowen


2 Answers

node.Attributes.Add("class","active");
like image 63
I4V Avatar answered Oct 07 '22 12:10

I4V


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.

like image 40
Arran Avatar answered Oct 07 '22 13:10

Arran