If you use a Frame control and navigate to a URI in WPF 3.5 SP1, you'll end up with instantiating the WebBrowser control under the hood, which is new in WPF 3.5 SP1. In order to get at it, you’ll need to cast the Content property of the Frame to a WebBrowser.  I recently had a situation where I needed to actually get at the DOM itself from the WebBrowser control However, I was initially mystified when I saw that the Document property returned a raw .NET object. What do I do with that?

What you need to do is add a reference to the COM type Microsoft HTML Object Library from Visual Studio. That will create a managed wrapper for the MSHTML.dll.  Then you can cast the object returned by the document property to a mshtml.HTMLDocumentClass.  Then you have full access to the DOM!

Oh, one other gotcha: make sure you don’t do this right after calling Navigate() on your frame, because  Navigate() is asynch. You need to do this after the frame’s ContentRendered event has fired.  And, because ContentRendered fires as soon as the Frame is loaded, it may not yet contain the WebBrowser control, so you need to do some type checking. Here’s a code snippet:

    void moddedFrame_ContentRendered(object sender, EventArgs e)
    {
        if (moddedFrame.Content.GetType() == typeof(WebBrowser))
        {
            WebBrowser webBrowser = (WebBrowser)moddedFrame.Content;
            mshtml.HTMLDocumentClass dom = (mshtml.HTMLDocumentClass)webBrowser.Document;
            Console.Write(dom.body.innerHTML);
        }
    }
Posted on November 12, 2008 09:25
Actions: E-mail | Comments (6)

Comments


xiaopeng wang

xiaopeng wang

November 11, 2008 10:07

Hi,
I can't get the dom and it was null: this is my codes

http://www.freep.cn/Default.aspx" rel="nofollow">http://www.freep.cn/Default.aspx" target="_blank" >http://www.freep.cn/Default.aspx" rel="nofollow">http://www.freep.cn/Default.aspx
          


xiaopeng wang

xiaopeng wang

November 11, 2008 15:16

Codes is here:
freep.cn/p.aspx?u=v20__p_0811131015083250_0.jpg" rel="nofollow">freep.cn/p.aspx?u=v20__p_0811131015083250_0.jpg" target="_blank" >freep.cn/p.aspx?u=v20__p_0811131015083250_0.jpg" rel="nofollow">freep.cn/p.aspx?u=v20__p_0811131015083250_0.jpg
          


GeekTieGuy

GeekTieGuy

November 11, 2008 23:58

Would be nice if stuff like this was in the documentation in addition to your illustrious blog... Thanks for posting! Good information.
          


Walter

Walter

November 12, 2008 14:13

currently Webbrowser doesn't work well when I set it to allowtransparency = true.

is there any plan for the product team to fix it?
          


Glenn

Glenn

November 14, 2008 01:59

re: WebBrowser disappears when allowtransparency=true:  we've gone thru our contacts at MS and "there are no firm plans to fix this, as deeply depends on complex legacy restrictions in technology".  It has to do with the way wpf renders windows vs the underlying webbrowser technology that uses win32 painting.
          


Walter

Walter

November 17, 2008 18:17

currently we are building a desktop gadget with WPF, unfortunately we need to change the storyboard because of this "bug". If product team able to fix it, it definately will be very COOL!
          


Add comment

Enter your name, handle, alias, or email.

We'll incarnate your avatar from the services below.