Htmlagilitypack Official document (vii) "Attributes"

Source: Internet
Author: User

Traversing allows you to traverse HTML nodes. Method

name Description
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'll all is 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 ' this node '. If The attribute is not found, it would be created automatically.
Public void Add (Htmlattribute item)

Adds the provided project to the collection. The Add method is a member parameter of the htmlagilitypack.htmlattributecollection :

Item: The property item to add.

The following example adds a property 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)

Adds a new property to the collection using the given value. The Add method is a member parameter of the htmlagilitypack.htmlattributecollection :

Name: The names of the properties to add.
Value: The values of the property to be added.

The following example adds a property item to the collection with the given 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)

Creates and inserts a new property as the last property in the collection. The Append method is a member parameter of the htmlagilitypack.htmlattributecollection :

Name: The names of the properties to insert. return:

The appended property.

The following example appends the specified property at the end.

var htmldoc = new HTMLDocument ();
htmldoc.loadhtml (HTML);

var h1node = HtmlDoc.DocumentNode.SelectSingleNode ("//h1");

H1Node.Attributes.Append ("bgcolor");
Public Htmlattribute Append (htmlattribute newattribute)

Inserts the specified property as the last property in the collection. The Append method is a member parameter of the htmlagilitypack.htmlattributecollection :

Newattribute: The property to insert. cannot be null. return:

The appended property.

The following example appends the specified property at the end.

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)

Creates and inserts a new property as the last property in the collection. The Append method is a member parameter of the htmlagilitypack.htmlattributecollection :

Name: The names of the properties to insert.
Value: The values of the property to be inserted. return:

The appended property.

The following example creates and inserts a new property as the last property in the collection.

var htmldoc = new HTMLDocument ();
htmldoc.loadhtml (HTML);

var h1node = HtmlDoc.DocumentNode.SelectSingleNode ("//h1");

H1Node.Attributes.Append ("Align", "center");
Public void Remove ()

Removes all attributes from the collection. The Remove method is a member of the htmlagilitypack.htmlattributecollection

The following example deletes all the properties in the collection.

var htmldoc = new HTMLDocument ();
htmldoc.loadhtml (HTML);

var h1node = HtmlDoc.DocumentNode.SelectSingleNode ("//h1");

H1Node.Attributes.Remove ();
Public void Remove (string name)

Deletes a property from the list by using the name. If the name has more than one attribute, they will all be deleted. The Remove method is a member parameter of the htmlagilitypack.htmlattributecollection :

Name: The names of the properties. cannot be null. Example

The following example deletes a property from the list by using the name.

var htmldoc = new HTMLDocument ();
htmldoc.loadhtml (HTML);

var h1node = HtmlDoc.DocumentNode.SelectSingleNode ("//h1");

H1Node.Attributes.Remove ("align");
Public void Remove (Htmlattribute attribute)

Deletes a given property from the list. The Remove method is a member parameter of the htmlagilitypack.htmlattributecollection :

Attribute: The property to be deleted. cannot be null. Example

The following example deletes the first property from the list.

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

Deletes all the properties in the list. The RemoveAll method is a member of the htmlagilitypack.htmlattributecollection

The following example deletes all the properties in the list.

var htmldoc = new HTMLDocument ();
htmldoc.loadhtml (HTML);

var h1node = HtmlDoc.DocumentNode.SelectSingleNode ("//h1");

H1Node.Attributes.RemoveAll ();
Public void RemoveAt (int index)

Deletes the property at the specified index. The RemoveAt method is a member parameter of the htmlagilitypack.htmlattributecollection :

Index: The indexes of the property to delete.

The following example deletes a property from the list at the specified 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)

Help class method to set the value of one attribute of this node. If this property is not found, it is created automatically. The Setattributevalue method is the member parameter of the * Htmlagilitypack.htmlnode * :

Name: The names of the properties to set. cannot be null.
Value: The values of the property.

The following example sets the property value.

var htmldoc = new HTMLDocument ();
htmldoc.loadhtml (HTML);

var h1node = HtmlDoc.DocumentNode.SelectSingleNode ("//h1");

H1Node.Attributes.Append ("style");

H1node.setattributevalue ("Style", "Color:blue");

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.