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

  • Home
  • SEARCH
  • 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 7603009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:33:32+00:00 2026-05-30T23:33:32+00:00

So I have an HTML snippet that I want to modify using C#. <div>

  • 0

So I have an HTML snippet that I want to modify using C#.

<div>
This is a specialSearchWord that I want to link to
<img src="anImage.jpg" />
<a href="foo.htm">A hyperlink</a>
Some more text and that specialSearchWord again.
</div>

and I want to transform it to this:

<div>
This is a <a class="special" href="http://mysite.com/search/specialSearchWord">specialSearchWord</a> that I want to link to
<img src="anImage.jpg" />
<a href="foo.htm">A hyperlink</a>
Some more text and that <a class="special" href="http://mysite.com/search/specialSearchWord">specialSearchWord</a> again.
</div>

I’m going to use HTML Agility Pack based on the many recommendations here, but I don’t know where I’m going. In particular,

  1. How do I load a partial snippet as a string, instead of a full HTML document?
  2. How do edit?
  3. How do I then return the text string of the edited object?
  • 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-05-30T23:33:33+00:00Added an answer on May 30, 2026 at 11:33 pm
    1. The same as a full HTML document. It doesn’t matter.
    2. The are 2 options: you may edit InnerHtml property directly (or Text on text nodes) or modifying the dom tree by using e.g. AppendChild, PrependChild etc.
    3. You may use HtmlDocument.DocumentNode.OuterHtml property or use HtmlDocument.Save method (personally I prefer the second option).

    As to parsing, I select the text nodes which contain the search term inside your div, and then just use string.Replace method to replace it:

    var doc = new HtmlDocument();
    doc.LoadHtml(html);
    var textNodes = doc.DocumentNode.SelectNodes("/div/text()[contains(.,'specialSearchWord')]");
    if (textNodes != null)
        foreach (HtmlTextNode node in textNodes)
            node.Text = node.Text.Replace("specialSearchWord", "<a class='special' href='http://mysite.com/search/specialSearchWord'>specialSearchWord</a>");
    

    And saving the result to a string:

    string result = null;
    using (StringWriter writer = new StringWriter())
    {
        doc.Save(writer);
        result = writer.ToString();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some html snippet <div id=title>This is title<span id=close>X<span><div> The width of this
Hi I have that html snippet <span class=container> <span class=inner> <span class=img_class> <img id=img1
I have a HTML snippet as follows, I want to take the 'article-form' div,
I have a ModelAdmin where I need to insert some html-snippet that is not
I have a some of html strings that my application generates. Each html 'snippet'
Imagine i have this HTML snippet 4 times over for four different sections of
I have a long snippet of HTML I want some javascript variable to equal
From this site: http://www.toymaker.info/Games/html/vertex_shaders.html We have the following code snippet: // transformations provided by
I have the following snippet in one of my html pages : <div class=inputboximage>
I have the following HTML (a snippet): <td class=ms-vb-title height=100%> <table class=ms-unselectedtitle onmouseover=OnItem(this) ctxname=ctx1

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.