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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:33:20+00:00 2026-05-26T07:33:20+00:00

I need to add a custom attribute with incrementing number to every HTML tag

  • 0

I need to add a custom attribute with incrementing number to every HTML tag in the document, similar to this question, but only in HTML, not XML file.

I tried to accomplish it with HTML Agility Pack, here is my code:

        HtmlDocument htmldoc = new HtmlDocument();
        htmldoc.LoadHtml(text);
        var num = 1;
        foreach (HtmlNode node in htmldoc.DocumentNode.DescendantNodes())
        {
            node.Attributes.Add("gist_num",(num++).ToString());
        }

        var numberedfilename = Path.GetDirectoryName(fname) + @"\" + Path.GetFileNameWithoutExtension(fname) + "-num.htm";

        htmldoc.Save(numberedfilename);

But I get a stack overflow exception here in HTML Agility Pack HtmlTextNode class.
I tried several ways to correct this bug by changing the class, but at no avail.

What would you suggest here?

— edit —
So, the exception is just “Stack Overflow” written to the console.

“Process is terminated due to StackOverflowException.”

Since it is Stack Overflow there is no possibility to get any stack values.
Here is the code where VS shows this exception happening:

    /// <summary>
    /// Gets or Sets the text of the node.
    /// </summary>
    public string Text
    {
        get
        {
            if (_text == null)
            {
                return base.OuterHtml;
            }
            return _text;
        }
         set { _text = value; }
    }

So, any ideas?

  • 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-26T07:33:21+00:00Added an answer on May 26, 2026 at 7:33 am

    You need to filter the nodes so you’re only selecting the elements. For some reason, going through the descendants in HTML Agility Pack includes other nodes like document and text nodes incorrectly. Since you’re blindly adding attributes to all nodes, it chokes serializing the non-element nodes.

    // note: Descendants() and DescendantNodes() is equivalent (unfortunately)
    var query = htmldoc.DocumentNode.Descendants()
        .Where(node => node.NodeType == HtmlNodeType.Element);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to add a custom attribute to the selected text in an NSTextView.
This is a pretty specific question, but I need to load a Silverlight application
I have an XSL style sheet for which I need to add some custom
I am making a custom control I need to add some default keybindings, microsoft
I have created a custom Attribute to decorate a number of classes that I
Can I add custom model attribute titles, so forms could automatically render them instead
I need to add a custom property to an Entity Framework class, however when
my first question is, where do I put this custom extension so that it
When serializing a custom generic collection to Xml how do I add an attribute
A similar question was asked here in storing information in a given html element.

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.