Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8530459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:15:02+00:00 2026-06-11T09:15:02+00:00

I have a need to retrieve the html elements that are contained within 2

  • 0

I have a need to retrieve the html elements that are contained within 2 other html elements using htmlagilitypack with C#.

As an example, I have the following:

<div id="div1" style="style definition here">
  <strong>
    <font face="Verdana" size="2">Your search request retrieved 0 matches.</font>
  </strong>
  <font face="Verdana" size="2">Some more text here.</font>
  <br><br>
  <!--more html here-->
</div>

I want to return everything between

<div id="div1">

and the first

<br>

without returning either of those elements.

I can’t get my head around the syntax required for this so if somebody could explain to me the best way to get the html that exists between 2 other known start tags while ignoring the end tags, I would really appreciate it.

I should also mention that I need to first find the div with the id of div1 within the surrounding html of a complete web page.

I don’t need the actual nodes to have reference equality with the nodes that came from a specific HtmlDocument, they just have to be the same content-wise.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T09:15:04+00:00Added an answer on June 11, 2026 at 9:15 am

    When HtmlNode instances are returned, multiple calls for the same node will produce the same reference. You can use this to your advantage (although it’s an implementation detail, so be careful).

    Basically, you’d get all the descendants that are elements up until the node in question. You select the node to start from:

    HtmlNode divNode = doc.DocumentNode.SelectSingleNode("div[@id='div1']");
    

    The node you want to go up to:

    // Note that in this case, working off the first node is not necessary, just
    // convenient for this example.
    HtmlNode brNode = divNode.SelectSingleNode("br");
    

    And then use the TakeWhile extension method on the Enumerable class to take all the elements up until the second element, like so:

    // The nodes.
    IEnumerable<HtmlNode> nodes = divNode.Descendants().
        TakeWhile(n => n != brNode).
        Where(n => n.NodeType == HtmlNodeType.Element);
    

    It’s the comparison in the TakeWhile method (n => n != brNode) that depends on reference comparison (that’s the implementation detail part).

    The last filter is to give you just element nodes, as that is what you’d typically get with calls to SelectSingleNode; if you want to process other node types, you can omit that.

    Cycling through those nodes like this:

    foreach (HtmlNode node in nodes)
    {
        // Print.
        Console.WriteLine("Node: {0}", node.Name);
    }  
    

    Produces:

    Node: strong
    Node: font
    Node: font
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following html.. 1 main div having 2 child divs. I need
i need to retrieve the page title from an URL i already have using
I need to retrieve a value for a clickable button using jquery. I have
Using this file as source, I have a situation where I need to retrieve
I have a page with an iframe that has an html document within it.
I have a webpage in HTML with these elements: <div class=content_page> <a href=/earth class=nametessera
I have a contentEditable div within which I need to allow the user to
Suppose, that I need to have a HTML control inside my Flex/AIR app. The
I have an HTML textbox that contains some SQL code that I need executed.
I need to retrieve the html contents (source) of the pages, for example: www.google.com

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.