Using MSHTML to modify HTML Elements

Hello,

My application composes an HTML page at run time using content pulled from the net. Once composed, it is saved to file and displayed in aWeb Browser control.

Once displayed I need to add the (TARGET="_blank") atribute to all anchor tags. I am using MSHTML as follows:

private void wbUserRSS_NavigateComplete2(object sender, AxSHDocVw.DWebBrowserEvents2_NavigateComplete2Event e)
{
      // Force all links in the document to open external windows

      mshtml.HTMLDocument      htmlDocument;

      htmlDocument      = (mshtml.HTMLDocument) this.wbUserRSS.Document;

      // How to access all the <A> elements in the document?
}

Also, are there any good articles out there explaining possibilities with MSHTML in an easy way (unlike the MSDN documentation)?

Thanks,

Khan
raheelasadkhanAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

raheelasadkhanAuthor Commented:
In simpler words, how do I enumerate all the elements within an HTMLDocument and determine what type of element each is?
vo1dCommented:
try this to process all anchors:

extend your eventhandler as follows:

private void wbUserRSS_NavigateComplete2(object sender, AxSHDocVw.DWebBrowserEvents2_NavigateComplete2Event e)
{
     // Force all links in the document to open external windows

     mshtml.HTMLDocument     htmlDocument;

     htmlDocument     = (mshtml.HTMLDocument) this.wbUserRSS.Document;

     foreach(mshtml.IHTMLDOMNode node in (htmlDocument.childNodes as mshtml.IHTMLDOMChildrenCollection))
     {
                ProcessNode(node);            
     }
}

now the ProcessNode method:

private void ProcessNode(mshtml.IHTMLDOMNode node)
{            
  if (node is mshtml.HTMLAnchorElement)
  {
    //make your changes here for an anchor
  }
  if ((node.childNodes as mshtml.IHTMLDOMChildrenCollection).length == 0)
  {
     return;
  }
  foreach (mshtml.IHTMLDOMNode childNode in (node.childNodes as mshtml.IHTMLDOMChildrenCollection))
  {
    ProcessNode(childNode);
  }
}

hope this will help.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
raheelasadkhanAuthor Commented:
Yes that looks about right. I'll give it a try and get back to you. Thanks.
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

raheelasadkhanAuthor Commented:
I added the following line to the processing section:
((mshtml.HTMLAnchorElement) node).target      = "_blank";


This seems to have no effect. The links that were opening in the same window before are stilldoing the same.
raheelasadkhanAuthor Commented:
My bad. My example can update the attribute value but does nothing if the attribute is not present. This works:
((mshtml.HTMLAnchorElement) node).setAttribute("TARGET", "_blank", 0);

Thanks again.

On another note, is there a good documentation source or article around with "How to's"? The MSDN documentation is very unfriendly for MSHTML.
vo1dCommented:
you should take a look after the page is loaded in the source of the page, i dont think that you had changed the correct tags, because the page is finished with loading and remains in memory.
raheelasadkhanAuthor Commented:
On your last comment, if you alter HTML at runtime via the DOM like we are doing, the changes DO NOT show up in 'View Source'. At least that was my observation.
vo1dCommented:
oh ok, great that it works. i dont know about any documentations for that component. i had done that example only with debugging and looking at the webbrowsers fields.
vo1dCommented:
maybe you could take a look in the msdn for net2, because microsoft integrated now a webbrowser component, which is a managed wrapper for the component, you used. maybe you can find more information there.
raheelasadkhanAuthor Commented:
Yeh, the switch to 2.0 is inevitable. Thanks again.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.