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 9147015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:53:08+00:00 2026-06-17T10:53:08+00:00

I would like to return the complete InnerText of a div where a specific

  • 0

I would like to return the complete InnerText of a div where a specific text is inside.

For example: I am searching for “hello world” in the HTMLDocument (HTMLAgilityPack) and want to return the complete InnerText of the div where “hello world” was found.

This is what I tried:

HtmlNode textNode = doc.DocumentNode.SelectNodes("//text()[contains(., 'hello world')]/..")

This returned the HtmlNode where the specific text was found.

Now I want to get the first parent div of the textNode to return the complete InnerText.

Thanks in advance

  • 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-17T10:53:09+00:00Added an answer on June 17, 2026 at 10:53 am

    This would do it I think…

    var nodes2 = doc.DocumentNode.SelectNodes("//div[contains(.//text(), 'Hello World')]");
    

    And this is probably an even better solution:

    var nodes3 = doc.DocumentNode.SelectNodes("//text()[contains(., 'Hello World')]/ancestor::div[1]");
    

    or use the Linq-to-XML notation:

            var nodes =
                doc.DocumentNode.Descendants("div")
                   .Where(
                       div =>
                       div.DescendantsAndSelf("text()").Where(text => text.InnerHtml.Contains("Hello World")).Any())
    

    Or

            var nodes4 = from div in doc.DocumentNode.Descendants("div")
                         from text in div.DescendantsAndSelf("text()")
                         where text.InnerText.Contains("Hello World")
                         let firstParent = text.AncestorsAndSelf("div").First()
                         select firstParent;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to use hilo generator but there is no any complete example
I would like to return the id of a newly created object for my
I would like to return an array of string in my web services I've
I have a MySQL table and would like to return the rows based on
I have a method where I would like to return an object instance of
I have a function that I would like to return 0 if a certain
I have a script with a factory method that I would like to return
I have created a dictionary in Class A and would like to return the
Given the following XML, I would like to return all eventtitles where the eventtype
Given the following directory: string fullpath = C:\MyDir1\MyDir2\MyDir3; I would like to return MyDir3

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.