Search Results for

    Show / Hide Table of Contents

    Class HtmlNode

    The HtmlNode class represents a single DOM element in a HTML or XML document.

    Inheritance
    System.Object
    HtmlNode
    Namespace: IronWebScraper
    Assembly: IronWebScraper.dll
    Syntax
    public class HtmlNode : Object

    HtmlNode is the DOM element you read when pulling data from a scraped page. Selector queries on a Response (Css, QuerySelectorAll, XPath, GetElementById) return HtmlNode instances or arrays, and each node exposes the element's text, markup, attributes, and position in the tree.

    Text comes out in two grades: InnerText and TextContent keep the raw element text, while InnerTextClean and TextContentClean collapse whitespace for tidy field values. Markup is available as InnerHtml and OuterHtml. Attributes are read through the Attributes dictionary or, for one value, GetAttribute("href"), with HasAtribute testing presence. Element identity is on NodeName and NodeType.

    Whitespace handling matters in practice: a title or price read from InnerText often carries newlines and indentation from the source markup, while the Clean variants return the trimmed, single-spaced value suited to a data field. OuterHtml includes the element's own tag, useful when storing a fragment, while InnerHtml returns only its contents, and NodeName reports the tag, such as div or a, when a selector matches mixed element types. When an attribute may be absent, guard with HasAtribute before GetAttribute, since a missing attribute returns null.

    A node is also a query root, so extraction can drill down without returning to the response. Css, QuerySelector, QuerySelectorAll, and XPath run against the node's own subtree, and ChildNodes and ParentNode walk the tree directly. A common shape selects a container with one query, then reads child fields from each node in the result.

    foreach (HtmlNode card in response.Css("div.product"))
    {
        string name = card.Css("h3").FirstOrDefault()?.TextContentClean;
        string price = card.GetAttribute("data-price");
        Scrape(new { name, price });
    }

    The shopping-site how-to extracts fields from nodes this way, and the web scraping tutorial covers selecting and reading elements.

    Properties

    Attributes

    Gets the attributes of the HTML node (such ash href, class, style etc).

    Declaration
    public Dictionary<string, string> Attributes { get; }
    Property Value
    Type Description
    System.Collections.Generic.Dictionary<System.String, System.String>

    The attributes as a Dictionary<string, string>

    ChildNodes

    An array of nodes nested within the HtmlNode.

    Declaration
    public HtmlNode[] ChildNodes { get; }
    Property Value
    Type Description
    HtmlNode[]

    InnerHtml

    Gets the HtmlNode's inner Html as a string. OuterHTML will not include the current tag's own html markup, only that nested inside it.

    Declaration
    public string InnerHtml { get; }
    Property Value
    Type Description
    System.String

    InnerText

    Gets the text content of the HtmlNode as might be shown to a user. Text will be trimmed, with all groups of whitespace turned into a single space character.

    Declaration
    public string InnerText { get; }
    Property Value
    Type Description
    System.String

    InnerTextClean

    Gets the text content of the HtmlNode as might be shown to a user.

    Declaration
    public string InnerTextClean { get; }
    Property Value
    Type Description
    System.String

    NodeName

    Name of the HtmlNode such as "div" "p" or "#text"

    Declaration
    public string NodeName { get; }
    Property Value
    Type Description
    System.String

    NodeType

    Type of the HtmlNode

    Possible values are: "ELEMENT_NODE", "TEXT_NODE" , "CDATA_SECTION_NODE", "COMMENT_NODE", "DOCUMENT_NODE", "DOCUMENT_TYPE_NODE"
    Declaration
    public string NodeType { get; }
    Property Value
    Type Description
    System.String

    OuterHtml

    Gets the HtmlNode's outer Html as a string. OuterHTML will include the tag's own html markup.

    Declaration
    public string OuterHtml { get; }
    Property Value
    Type Description
    System.String

    ParentNode

    Gets the Parent (enclosing) tag for this HtmlNode.

    Declaration
    public HtmlNode ParentNode { get; }
    Property Value
    Type Description
    HtmlNode

    The parent node as an HtmlNode.

    TextContent

    Gets the text content of the HtmlNode and all of its descendants.

    Declaration
    public string TextContent { get; }
    Property Value
    Type Description
    System.String

    TextContentClean

    Gets the text content of the HtmlNode, Trimmed, with all groups of whitespace turned into a single space character. TextContent will include the content of non-printable elements just as style and script nodes.

    Declaration
    public string TextContentClean { get; }
    Property Value
    Type Description
    System.String

    Methods

    Css(String)

    Uses CSS selectors to find all child nodes matching selector. This works in the same way as $('.ClassName') in jQuery or querySelectorAll() in JavaScript.

    Declaration
    public HtmlNode[] Css(string selector)
    Parameters
    Type Name Description
    System.String selector
    Returns
    Type Description
    HtmlNode[]

    CssExists(String)

    Uses CSS selectors to find if there are any matching nodes within the Response Document. This works in the same way as $('.ClassName').length > 0 in jQuery or querySelectorAll().length > 0 in JavaScript.

    Declaration
    public bool CssExists(string selector)
    Parameters
    Type Name Description
    System.String selector
    Returns
    Type Description
    System.Boolean

    Equals(Object)

    Method to assert if the object is equal to this HtmlNode

    Declaration
    public override bool Equals(object obj)
    Parameters
    Type Name Description
    System.Object obj
    Returns
    Type Description
    System.Boolean

    true if object is equal to this HtmlNode and false if not

    GetAttribute(String)

    Gets a single attribute vale from the HtmlNode. Returns null if that attribute is not present.

    Declaration
    public string GetAttribute(string attribute)
    Parameters
    Type Name Description
    System.String attribute
    Returns
    Type Description
    System.String

    GetElementById(String)

    Synonym of JavaScript's getElementById function. Searches inside the current HtmlNode.

    Declaration
    public HtmlNode GetElementById(string id)
    Parameters
    Type Name Description
    System.String id
    Returns
    Type Description
    HtmlNode

    GetElementsByTagName(String)

    Synonym of JavaScript's getElementsByTagName function. Searches inside the current HtmlNode.

    Declaration
    public HtmlNode[] GetElementsByTagName(string tagName)
    Parameters
    Type Name Description
    System.String tagName
    Returns
    Type Description
    HtmlNode[]

    GetHashCode()

    Method to get a HashCode of this HtmlNode

    Declaration
    public override int GetHashCode()
    Returns
    Type Description
    System.Int32

    HasAtribute(String)

    Tests if the HtmlNode has a given attribute such as "class" or "href"

    Declaration
    public bool HasAtribute(string attribute)
    Parameters
    Type Name Description
    System.String attribute
    Returns
    Type Description
    System.Boolean

    QuerySelector(String)

    Synonym of JavaScript's querySelector function. Searches inside the current HtmlNode.

    Declaration
    public HtmlNode QuerySelector(string selector)
    Parameters
    Type Name Description
    System.String selector
    Returns
    Type Description
    HtmlNode

    QuerySelectorAll(String)

    Synonym of JavaScript's querySelectorAll function. Searches inside the current HtmlNode.

    Declaration
    public HtmlNode[] QuerySelectorAll(string selector)
    Parameters
    Type Name Description
    System.String selector
    Returns
    Type Description
    HtmlNode[]

    XPath(String)

    Queries the HtmlNode to return all descendant nodes matching an XPath expression.

    Declaration
    public HtmlNode[] XPath(string expression)
    Parameters
    Type Name Description
    System.String expression
    Returns
    Type Description
    HtmlNode[]
    ☀
    ☾
    Downloads
    • Download with Nuget
    • Start for Free
    In This Article
    Back to top
    Install with Nuget