troubleshooting Question

How to replace a HTML-tags inner text content using C#!

Avatar of nelshh
nelshh asked on
Web Components
1 Comment1 Solution917 ViewsLast Modified:

Right now I'm working on a Internet Explorer add on which is supposed to scan a HTML-document for URL's in plain text, and then "linkify" them.

I have access to the websites DOM, and had an idea to traverse all of the DOM nodes and search for "links" using RegEx, to replace these text with HTML-code, however, when changing the "InnerText" property of the IHTMLElement object, all of it's child nodes are lost, which seriously destroys the website.

Here's some code, that nearly works, but only for nodes that doesn't have any children.

Can anyone please help me with this problem, I would be very grateful!


//This method is called when IE has finished loading a page
void _webBrowser2Events_DocumentComplete(object pDisp, ref object URL)
    if (pDisp == _webBrowser2)
        HTMLDocument pageContent = _webBrowser2.Document;
        IHTMLElement bodyHtmlElmnt = pageContent.body;
And here's the fixElement-method:

void fixElement(IHTMLElement node)
    if (node.innerText!=null && ((IHTMLElementCollection)node.children).length==0)
        node.innerText= node.innerText.Replace("testString", "replaceWithThis");

    foreach (IHTMLElement child in (node.children as mshtml.IHTMLElementCollection))
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 1 Comment.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 1 Comment.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros