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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:08:21+00:00 2026-06-08T20:08:21+00:00

I have some html mixed with text and links and I would like to

  • 0

I have some html mixed with text and links and I would like to extract the text as it is with the linked words without having to remove the links and do some crazy thing later to put it back in at the same place.

The HTML looks like this:

<div id="i want what is inside here">
<h3>some text</h3>
<div>more text with a <a href="url">link</a></div>
<p>some more text<br />
<a href="url">another link</a> here...</p>
</div>

And the output I wanted is:

some text
more text with a <a href="url">link</a>
some more text
<a href="url">another link</a> here...

I know how to extract the text using HTMLAgilityPack recently I’ve learned lots of new things on how to use ancestor, preceed on the xpath and some other things and it made me wonder:

Is it possible using xpath to get the desired output mentioned above or what how should I do it ?

If there is no xpath condition that is usable to this I was thinking of extracting the text then extracting the links and replace the link inner text matchs on the text with links (not reliable I assume) but it was one way I thinked that could be done, what would you advise me to do ?

  • 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-08T20:08:22+00:00Added an answer on June 8, 2026 at 8:08 pm

    XPath, the XML Path Language, is a query language for selecting nodes
    from an XML document.

    You need to transform your document according to your rules. You could use xpath to select nodes which should be transformed, but xpath couldn’t be used to perform this transformation.

    To do it, you could iterate through the document nodes from the most deep to the root node, and if this is not an <a> tag, replace it with the inner HTML of it.

    Fortunately, it seems that AgilityPack enumerates nodes in the order the nodes found in the document. This means the necessary order can be retrieved by reversing the list of nodes. See:

    // getting the non-anchor nodes in the reversed order
    var nodes = doc.DocumentNode.SelectNodes("//*[name()!='a']")
        .Reverse()
        .ToList();
    
    // replacing with the inner html
    foreach (var node in nodes)
    {
        var replacement = doc.CreateTextNode(node.InnerHtml);
        node.ParentNode.ReplaceChild(replacement, node);
    }
    
    // and getting the output
    var output = doc.DocumentNode.OuterHtml;
    

    This will get you something like this:

    some text
    more text with a <a href='url'>link</a>
    some more text
    <a href='url'>another link</a> here...
    

    But note, after the transformation the document became the whole text node. If you need to work with it as HTML fragment, you’ll have to create a new document from the modified one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some html like the following: <div class=control-group> <input type=text data-bind=value: $data.DealCode name=DealCode
I have some Html like: <div id=adiv> <ul> <li data-id=3>Cat</li> <li data-id=4>Dog</li> </ul> </div>
I have some html files and want to extract only lines with containing these
I have some html files that I want to convert to text. I have
I have some HTML that looks like this: <div> <div class=value> <a href=# class=clicker>Some
I have some html code having hierarchy as <div id=a> <span> <span id=s1>...</span> <span
I have an xml document that contains some html. <begin-line> <verse-num>6</verse-num>a mixed people<footnote id=f2>
I have some HTML like this (which I cannot change): <div> <p class=name> <span>Employee
I have some HTML that would have elements with multiple classes, and I need
I have some HTML code with a dialog inside, like this: <center> <a href=#add_auto

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.