本文介绍了HTML Agility Pack-新的HtmlAttribute的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中使用HTML Agility Pack,我有一个节点想要向其中添加属性.

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

当前该节点是一个没有属性的<li>元素,我想为其添加一个活动"类.

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

看起来最好用的是node.Attributes.Add(attrClass)

其中attrClassclass="active"HtmlAttribute.

但是,如果我尝试定义一个新的HtmlAttribute,则会收到一条错误消息,指出它没有任何构造函数.例如HtmlAttribute attrClass = new HtmlAttribute();

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

我的Html Agility Pack参考是否有问题,或者我做错了什么?

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?

推荐答案

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

这篇关于HTML Agility Pack-新的HtmlAttribute的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-24 11:34