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