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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:15:00+00:00 2026-05-25T15:15:00+00:00

I want to be able to add attributes to a string of html without

  • 0

I want to be able to add attributes to a string of html without having to build a parser to handle the html. In one specific case, I want to be able to extract the id of the html or insert an id to the html server side.

Say I have:

string stringofhtml = "<img src=\"someimage.png\" alt=\"the image\" />";

I would like to be able to do something like:

HtmlGenericControl htmlcontrol = new HtmlGenericControl(stringofhtml);
htmlcontrol.Attributes["id'] = "newid";

OR

int theid = htmlcontrol.Attributes["id"];

This is just a way that I can access/add attributes of the html strings that I have.

  • 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-25T15:15:00+00:00Added an answer on May 25, 2026 at 3:15 pm

    I do not think there is a control available which will provide you with the functionality you are looking for.

    Below I have made use of the HtmlAgility pack to parse/query the HTML and created a new control subclassing the Literal control.

    This control accepts an HTML string, checks to ensure it contains at least a single element and provides access to get/set that elements attributes.

    Example usage

    string image = "<img src=\"someimage.png\" alt=\"the image\" />";
    
    HtmlControlFromString htmlControlFromString = new HtmlControlFromString(image);
    
    htmlControlFromString.Attributes["id"] = "image2";
    
    string id = htmlControlFromString.Attributes["id"];
    

    Control

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI.WebControls;
    using HtmlAgilityPack;
    
    public class HtmlControlFromString : Literal
    {
        private HtmlDocument _document = new HtmlDocument();
        private HtmlNode _htmlElement;
    
        public AttributesCollection Attributes { get; set; }
    
        public HtmlControlFromString(string html)
        {
            _document.LoadHtml(html);
            if (_document.DocumentNode.ChildNodes.Count > 0)
            {
                _htmlElement = _document.DocumentNode.ChildNodes[0];
    
                Attributes = new AttributesCollection(_htmlElement);
                Attributes.AttributeChanged += new EventHandler(Attributes_AttributeChanged);
    
                SetHtml();
            }
            else
            {
                throw new InvalidOperationException("Argument does not contain a valid html element.");
            }
        }
    
        void Attributes_AttributeChanged(object sender, EventArgs e)
        {
            SetHtml();
        }
    
        void SetHtml()
        {
            Text = _htmlElement.OuterHtml;
        }
    }
    
    public class AttributesCollection
    {
        public event EventHandler AttributeChanged;
    
        private HtmlNode _htmlElement;
    
        public string this[string attribute]
        {
            get
            {
                HtmlAttribute htmlAttribute = _htmlElement.Attributes[attribute];
                return htmlAttribute == null ? null : htmlAttribute.Value;
            }
            set
            {
                HtmlAttribute htmlAttribute = _htmlElement.Attributes[attribute];
                if (htmlAttribute == null)
                {
                    htmlAttribute = _htmlElement.OwnerDocument.CreateAttribute(attribute);
                    htmlAttribute.Value = value;
                    _htmlElement.Attributes.Add(htmlAttribute);
                }
                else
                {
                    htmlAttribute.Value = value;
                }
    
                EventHandler attributeChangedHandler = AttributeChanged;
                if (attributeChangedHandler != null)
                    attributeChangedHandler(this, new EventArgs());
            }
        }
    
        public AttributesCollection(HtmlNode htmlElement)
        {
            _htmlElement = htmlElement;
        }
    }
    

    Hope this helps.

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

Sidebar

Related Questions

I want to be able to add scripting functionality to my application. One of
I want to be able to add a Binding to some properties of a
I want to be able to add a little note, at the beginning of
I want to be able to add 1 cell to the top of a
So I want to be able to add/remove class methods at runtime. Before you
I am working with MapKit and want to be able to add a (NSString
I have a scraping object basically. I want to be able to add POST
What I want to do is be able to add a file to my
I want to add a click able button to a MKAnnotation as seen in
I want to implement a feature where user can add new attributes to his

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.