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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:08:20+00:00 2026-06-01T05:08:20+00:00

I want to add a link to every image in the HTML with the

  • 0

I want to add a link to every image in the HTML with the value of the “href” attribute be the “src” attribute of the image. Namely changing the

 "<p> <img src="test.jpg"/></p>"

to

<p><a href="test.jpg"><img src="jpg"/></a></p>

And my code coming:

using HtmlAgilityPack;
var imgs = document.DocumentNode.Descendants("img");
foreach (HtmlNode node in imgs)
{
    if (node.ParentNode.Name != "a")
    {
        string replaceStr = string.Format("<a href=\"{0}\">{1}</a>", node.GetAttributeValue("src", null), node.OuterHtml);
        //node.OuterHtml= replaceStr;   It doesn't work, the outerHtml is readonly
        //node.ParentNode.RemoveChild(node, true);
    }
}

So how should I modify my code to make it work?

Updated:
after updating my code:

var imgs = document.DocumentNode.Descendants("img");
            foreach (var node in imgs)
            {
                if (node.ParentNode.Name != "a")
                {
                    var a = document.CreateElement("a");
                    a.SetAttributeValue("href", node.GetAttributeValue("src", null));
                    a.ChildNodes.Add(node);
                    node.ParentNode.ReplaceChild(a, node);
                }
            }

An error shows up “Unhandled InvalidOperationException “.

enter image description here

  • 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-01T05:08:21+00:00Added an answer on June 1, 2026 at 5:08 am

    First, you could use xpath to select <img> nodes whose parent node is not <a>:

    var imgs = doc.DocumentNode.SelectNodes("//img[not(parent::a)]").ToList();
    

    Then you should iterate over these nodes. On each iteration step just create a new ” element, appent iteration ” to it, and then replace this ” with newly created <a>:

    foreach (var img in imgs)
    {
        var a = doc.CreateElement("a");
        a.SetAttributeValue("href", img.GetAttributeValue("src", null));
        a.ChildNodes.Add(img);
        img.ParentNode.ReplaceChild(a, img);
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a html response message that I want to add a link to.
I want to add custom attribute settings to a generated anchor link of Wordpress.
I want to add this code: <link rel=stylesheet type=text/css href=css/global.css /> <!--[if lt IE
When I try to add a link of Facebook, it reads every image as
I want to postfix every link in a HTML file with a Google Analytics
I want to add a link to a PDF file but instead of printing
I want to be able to add new sections (via the 'add' link) and
I want add label to every row in tableview at right side of the
Question 1: I want to fade each href or img tag as they are
I want to add an element after every like button (chrome extension). Since posts

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.