Htmlagilitypack Official Document (iv) "traversing"

Source: Internet
Author: User

Traversing allows you to traverse HTML nodes. Property

name Description
ChildNodes Gets all the children of the node.
FirstChild Gets the The node.
LastChild Gets the last child of the node.
NextSibling Gets the HTML node immediately following this element.
ParentNode Gets the parent of this node (for nodes that can have parents).
Method
name Description
Ancestors () Gets all the ancestor of the node.
Ancestors (String) Gets ancestors with matching name.
Ancestorsandself () Gets all anscestor nodes and the current node.
Ancestorsandself (String) Gets all anscestor nodes and the current node with matching name.
Descendantnodes Gets all descendant nodes for this node and each of the child nodes
Descendantnodesandself Returns A collection of all descendant nodes the this element, in the document order
Descendants () Gets all descendant nodes in enumerated list
Descendants (String) Get all descendant nodes with matching name
Descendantsandself () Returns A collection of all descendant nodes the this element, in the document order
Descendantsandself (String) Gets all descendant nodes including this node
Element Gets-generation child node matching name
Elements Gets matching-generation child nodes matching name
Public htmlnodecollection childnodes {get;}

Gets all child nodes of the node. ChildNodes is a member of the Htmlagilitypack.htmlnode .

The following example displays all child elements of a node.

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

var htmlBody = HtmlDoc.DocumentNode.SelectSingleNode ("//body");

Htmlnodecollection childnodes = htmlbody.childnodes;

foreach (var node in childnodes)
{
    if node. NodeType = = htmlnodetype.element)
    {
        Console.WriteLine (node. outerhtml);
    }
   
Public Htmlnode firstchild {get;}

Gets the first child node of the node. FirstChild is a member of the Htmlagilitypack.htmlnode .

The following example displays the first child node of a node.

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

var htmlnodes = htmlDoc.DocumentNode.SelectNodes ("//body");

var htmlBody = HtmlDoc.DocumentNode.SelectSingleNode ("//body");

Htmlnode firstchild = htmlbody.firstchild;

Console.WriteLine (firstchild.outerhtml);    
Public Htmlnode LastChild {get;}

Gets the last child node of the node. LastChild is a member of the Htmlagilitypack.htmlnode .

The following example displays the last child node of a node.

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

var htmlnodes = htmlDoc.DocumentNode.SelectNodes ("//body");

var htmlBody = HtmlDoc.DocumentNode.SelectSingleNode ("//body");

Htmlnode lastchild = Htmlbody.lastchild;

Public Htmlnode NextSibling {get;}

Gets the HTML node immediately following this element. NextSibling is a member of the Htmlagilitypack.htmlnode .

The following example displays the next sibling node.

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

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

Htmlnode sibling = node. NextSibling;

while (sibling!= null)
{
    if (sibling). NodeType = = htmlnodetype.element)
        Console.WriteLine (sibling. outerhtml);

    Sibling = sibling. NextSibling;
}
Public IEnumerable < Htmlnode > Ancestors ()

Gets all ancestors of the node. The Ancestors method is returned by a member of the htmlagilitypack.htmlnode :

Returns a collection of all ancestor nodes for this element.

The following example displays the names of all ancestors.

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

var node = htmlDoc.DocumentNode.SelectSingleNode ("//b");

foreach (var nnode in node. Ancestors ())
{
    if (Nnode.nodetype = = htmlnodetype.element)
    {
        Console.WriteLine (nnode.name);
    }
}
Public ienumerable

Gets the ancestor with the matching name. The Ancestors method is a member parameter of the htmlagilitypack.htmlnode :

Name: The names of ancestor nodes. return:

Returns a collection of all ancestor nodes of this element with a matching name.

The following example displays the name of an ancestor with a matching name.

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

var node = htmlDoc.DocumentNode.SelectSingleNode ("//b");

foreach (var nnode in node. Ancestors ("Body")
{
    if (Nnode.nodetype = = htmlnodetype.element)
    {
        Console.WriteLine ("Node Name:" + Nnode.name);
        Console.WriteLine (Nnode.innertext);
    } 
Public ienumerable

Gets all ancestors of the node and the node itself. The Ancestorsandself method is returned by a member of the htmlagilitypack.htmlnode :

Also returns a collection of all ancestor nodes and nodes themselves. Example

The following example displays the names of the selected node and all of its ancestors.

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

var node = htmlDoc.DocumentNode.SelectSingleNode ("//u");

 foreach (var nnode in node. Ancestorsandself ())
 {
     if (Nnode.nodetype = = htmlnodetype.element)
     {
         Console.WriteLine ( nnode.name);
     }
       
Public ienumerable

Gets the ancestors and the node itself that have a matching name. The Ancestorsandself method is a member parameter of the htmlagilitypack.htmlnode :

Name: The names of ancestor nodes. return:

Returns a collection of names that match all ancestor nodes and the node itself.

The following example displays the name of the selected node and all ancestors with a matching name

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

var node = htmlDoc.DocumentNode.SelectSingleNode ("//u");

foreach (var nnode in node. Ancestorsandself ("P"))
{
    if (Nnode.nodetype = = htmlnodetype.element)
    {
        Console.WriteLine ( nnode.name);
    }
Public ienumerable

Gets all descendant nodes for this node and for each child node. The Descendantnodes method is a member parameter of the htmlagilitypack.htmlnode :

Levels: The depth level of the node to resolve in the HTML tree. return:

Returns a collection of all descendant nodes for this element. Example

The following example displays the names of all descendant nodes.

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

var node = htmlDoc.DocumentNode.SelectSingleNode ("//body");

foreach (var nnode in node. Descendantnodes ())
{
    if (Nnode.nodetype = = htmlnodetype.element)
    {
        Console.WriteLine (nnode.name);
    }
}
Public ienumerable

Gets the collection of all descendant nodes for this element in document order. The Descendantnodesandself method is returned by a member of the htmlagilitypack.htmlnode :

Returns a collection of all descendant nodes for this element in document order.

The following example displays the name of all descendant nodes and the node itself.

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

var node = htmlDoc.DocumentNode.SelectSingleNode ("//body");

foreach (var nnode in node. Descendantnodesandself ())
{
    if (Nnode.nodetype = = htmlnodetype.element)
    {
        Console.WriteLine ( nnode.name);
    }
Public ienumerable

Gets all descendant nodes in the enumerated list. Descendants is the member parameter of * Htmlagilitypack.htmlnode * :

Levels: The depth level of the node to resolve in the HTML tree. return:

Returns a collection of all descendant nodes for this element.

The following example displays the names of all descendant nodes.

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

var node = htmlDoc.DocumentNode.SelectSingleNode ("//body");

foreach (var nnode in node. Descendants ())
{
    if (Nnode.nodetype = = htmlnodetype.element)
    {
        Console.WriteLine (nnode.name);
    }
}
Public ienumerable

Gets all descendant nodes that have a matching name. Descendants is a member parameter of the htmlagilitypack.htmlnode :

Name: the names of descendant nodes. return:

Returns a collection of all descendant nodes that have a name match.

The following example displays the names of all descendant nodes that have a matching name.

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

var node = htmlDoc.DocumentNode.SelectSingleNode ("//body");

foreach (var nnode in node. Descendants ("H2"))
{
    if (Nnode.nodetype = = htmlnodetype.element)
    {
        Console.WriteLine (nnode.name) ;
    }
}
Public ienumerable

Gets the collection of all descendant nodes for this element in document order. The Descendantsandself method is returned by a member of the htmlagilitypack.htmlnode :

Returns a collection of all descendant nodes for this element in document order.

The following example displays the names of all descendants and the node itself.

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

var node = htmlDoc.DocumentNode.SelectSingleNode ("//body");

foreach (var nnode in node. Descendantsandself ())
{
    if (Nnode.nodetype = = htmlnodetype.element)
    {
        Console.WriteLine ( nnode.name);
    }
Public IEnumerable < Htmlnode > descendantsandself (string name)

Gets all descendant nodes with a matching name and the node itself. Descendantsandself is a member parameter of the htmlagilitypack.htmlnode :

Name: the names of descendant nodes. return:

Returns a collection of all descendants with a matching name and the node itself.

The following example displays the name of all descendant nodes with a matching name and the node itself.

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

var node = htmlDoc.DocumentNode.SelectSingleNode ("//body");

foreach (var nnode in node. Descendantsandself ("H2"))
{
    if (Nnode.nodetype = = htmlnodetype.element)
    {
        Console.WriteLine ( nnode.outerhtml);
    }
Public Htmlnode Element (string name)

Gets the first generation of child nodes with a matching name. The element method is a member parameter of the htmlagilitypack.htmlnode :

Name: The names of the child nodes. return:

Returns the first generation of child nodes with a matching name.

The following example displays the first generation of child nodes with a matching name.

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

var htmlBody = HtmlDoc.DocumentNode.SelectSingleNode ("//body");

var node = htmlbody.element ("P");

Console.WriteLine (node. outerHTML);
Public IEnumerable < Htmlnode > Elements (string name)

Gets the first generation of child nodes with a matching name. The elements method is a member parameter of the htmlagilitypack.htmlnode :

Name: The names of the child nodes. return:

Returns a collection of first-generation child nodes with a matching name.

The following example displays the first generation of child nodes with a matching name.

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

var htmlBody = HtmlDoc.DocumentNode.SelectSingleNode ("//body");

var nodes = htmlbody.elements ("H2");

foreach (var node in nodes)
{
    if node. NodeType = = htmlnodetype.element)
    {
        Console.WriteLine (node. outerhtml);
    }

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.