HtmlAgilityPack官方文档(七)【Attributes】

Traversing 允许您遍历HTML节点.

方法

名称描述
Add(HtmlAttribute)Adds supplied item to collection
Add(String, String)Adds a new attribute to the collection with the given values
Append(String)Creates and inserts a new attribute as the last attribute in the collection.
Append(HtmlAttribute)Inserts the specified attribute as the last attribute in the collection.
Append(String, string)Creates and inserts a new attribute as the last attribute in the collection.
Remove()Removes all attributes from the collection
Remove(String)Removes an attribute from the list, using its name. If there are more than one attributes with this name, they will all be removed.
Remove(HtmlAttribute)Removes a given attribute from the list.
RemoveAll()Remove all attributes in the list.
RemoveAt()Removes the attribute at the specified index.
SetAttributeValue()Helper method to set the value of an attribute of this node. If the attribute is not found, it will be created automatically.

public void Add(HtmlAttribute item)

将所提供的项目添加到集合中。Add方法是HtmlAgilityPack.HtmlAttributeCollection 的成员

参数:

item: 要添加的属性项目。

以下示例添加属性项。

var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(html);

var h1Node = htmlDoc.DocumentNode.SelectSingleNode("//h1");

Console.WriteLine(h1Node.OuterHtml);

var attribute = htmlDoc.CreateAttribute("style", "color: red");

h1Node.Attributes.Add(attribute);

public void Add(string name, string value)

使用给定的值向集合添加一个新属性。 Add方法是HtmlAgilityPack.HtmlAttributeCollection 的成员

参数:

name:要添加的属性的名称。
value: 要添加的属性的值。

以下示例将属性项添加到具有给定值的集合中。

var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(html);

var h1Node = htmlDoc.DocumentNode.SelectSingleNode("//h1");

h1Node.Attributes.Add("style", "color: red");

public HtmlAttribute Append(string name)

创建并插入一个新属性作为集合中的最后一个属性。Append方法是HtmlAgilityPack.HtmlAttributeCollection的成员

参数:

name: 要插入的属性的名称。

返回:

追加的属性。

以下示例在最后追加指定的属性。

var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(html);

var h1Node = htmlDoc.DocumentNode.SelectSingleNode("//h1");

h1Node.Attributes.Append("bgcolor");

public HtmlAttribute Append(HtmlAttribute newAttribute)

插入指定的属性作为集合中的最后一个属性。Append方法是HtmlAgilityPack.HtmlAttributeCollection 的成员

参数:

newAttribute: 要插入的属性。不可能为null。

返回:

追加的属性。

以下示例在最后追加指定的属性。


var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(html);

var h1Node = htmlDoc.DocumentNode.SelectSingleNode("//h1");

var attribute = htmlDoc.CreateAttribute("align", "left");

h1Node.Attributes.Append(attribute);

public HtmlAttribute Append(string name, string value)

创建并插入一个新属性作为集合中的最后一个属性。Append方法是HtmlAgilityPack.HtmlAttributeCollection 的成员

参数:

name: 要插入的属性的名称。
value: 要插入的属性的值。

返回:

追加的属性。

以下示例创建并插入一个新属性作为集合中的最后一个属性。

var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(html);

var h1Node = htmlDoc.DocumentNode.SelectSingleNode("//h1");

h1Node.Attributes.Append("align", "center");

public void Remove()

从集合中删除所有属性。 Remove方法是HtmlAgilityPack.HtmlAttributeCollection 的成员

以下示例删除集合中的所有属性。


var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(html);

var h1Node = htmlDoc.DocumentNode.SelectSingleNode("//h1");

h1Node.Attributes.Remove();

public void Remove(string name)

使用名称从列表中删除一个属性。 如果这个名称有多个属性,它们将全部被删除。Remove方法是HtmlAgilityPack.HtmlAttributeCollection的成员

参数:

name: 属性的名称。不可能为null。

例子

以下示例使用名称从列表中删除一个属性。


var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(html);

var h1Node = htmlDoc.DocumentNode.SelectSingleNode("//h1");

h1Node.Attributes.Remove("align");

public void Remove(HtmlAttribute attribute)

从列表中删除一个给定的属性。Remove方法是HtmlAgilityPack.HtmlAttributeCollection 的成员

参数:

attribute: 要删除的属性。不可能为null。

例子

以下示例从列表中删除第一个属性。


var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(html);

var h1Node = htmlDoc.DocumentNode.SelectSingleNode("//h1");

var attr = h1Node.Attributes.First();

h1Node.Attributes.Remove(attr);

public void RemoveAll()

删除列表中的所有属性。RemoveAll方法是HtmlAgilityPack.HtmlAttributeCollection的成员

以下示例将删除列表中的所有属性。


var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(html);

var h1Node = htmlDoc.DocumentNode.SelectSingleNode("//h1");

h1Node.Attributes.RemoveAll();

public void RemoveAt(int index)

删除指定索引处的属性。RemoveAt方法是HtmlAgilityPack.HtmlAttributeCollection 的成员

参数:

index: 要删除的属性的索引。

以下示例从指定索引处的列表中删除一个属性。

var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(html);

var h1Node = htmlDoc.DocumentNode.SelectSingleNode("//h1");

h1Node.Attributes.RemoveAt(1);

public HtmlAttribute SetAttributeValue(string name, string value)

帮助类方法来设置这个节点的一个属性的值。如果没有找到该属性,它将自动创建。SetAttributeValue方法是* HtmlAgilityPack.HtmlNode *的成员

参数:

name: 要设置的属性的名称。不可能为null。
value: 属性的值。

以下示例设置属性值。

var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(html);

var h1Node = htmlDoc.DocumentNode.SelectSingleNode("//h1");

h1Node.Attributes.Append("style");

h1Node.SetAttributeValue("style", "color:blue");
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值