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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:14:53+00:00 2026-05-26T12:14:53+00:00

I traverse an html document with SGML and XmlDocument. When I find an XmlNode

  • 0

I traverse an html document with SGML and XmlDocument. When I find an XmlNode which its type is Text, I need to change its value that has an xml element. I can’t change InnerXml because it’s readonly. I tried to change InnerText, but this time tag descriptor chars < and > encoded to &lt; and &gt;. for example:

<p>
    This is a text that will be highlighted.
    <anothertag />
    <......>
</p>

I’m trying to change to:

<p>
    This is a text that will be <span class="highlighted">highlighted</span>.
    <anothertag />
    <......>
</p>

What is the easiest way to modify the value of a text XmlNode?

  • 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-26T12:14:53+00:00Added an answer on May 26, 2026 at 12:14 pm

    I have a workaround, I don’t know it is a real solution or what, but it can result what I want. Please comment for this code if it is worthy solution or not

        private void traverse(ref XmlNode node)
        {
            XmlNode prevOldElement = null;
            XmlNode prevNewElement = null;
            var element = node.FirstChild;
            do
            {
                if (prevNewElement != null && prevOldElement != null)
                {
                    prevOldElement.ParentNode.ReplaceChild(prevNewElement, prevOldElement);
                    prevNewElement = null;
                    prevOldElement = null;
                }
                if (element.NodeType == XmlNodeType.Text)
                {
                    var el = doc.CreateElement("text");
                    //Here is manuplation of the InnerXml.
                    el.InnerXml = element.Value.Replace(a_search_term, "<b>" + a_search_term + "</b>");
                    //I don't replace element right now, because element.NextSibling will be null.
                    //So I replace the new element after getting the next sibling.
                    prevNewElement = el;
                    prevOldElement = element;
                }
                else if (element.HasChildNodes)
                    traverse(ref element);
            }
            while ((element = element.NextSibling) != null);
            if (prevNewElement != null && prevOldElement != null)
            {
                prevOldElement.ParentNode.ReplaceChild(prevNewElement, prevOldElement);
            }
    
        }
    

    Also, I remove <text> and </text> strings after the traverse function:

            doc = new XmlDocument();
            doc.PreserveWhitespace = true;
            doc.XmlResolver = null;
            doc.Load(sgmlReader);
            var html = doc.FirstChild;
            traverse(ref html);
            textBox1.Text = doc.OuterXml.Replace("<text>", String.Empty).Replace("</text>", String.Empty);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We use jQuery to parse some HTML. I then need to traverse that document
Assuming I have the following html div which I want to replicate its children,
I'm building a spider which will traverse various sites and data mining them. Since
I'm trying to traverse an AJAX response, which contains a remote web page (an
Which is the best way to convert xml to html, currently i am using
In an application I write that uses a large HTML table of numbers the
As most (all?) PHP libraries that do HTML sanitization such as HTML Purifier are
I've got a form that's a few pages long. To traverse the form all
There's a bug in Traverse() that's causing it to iterate nodes more than once.
I'm trying to access XML content attached to the end of a HTML document

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.