Help with webbrowser component

locdang used Ask the Experts™

I am attempting to get the entire page that is currently loaded in my webbrowser component loaded as html into a textbox.

however the webbrowser.DocumentText property doesn't return the innerhtml of and iframe controls in the page. How can I return the inner html of iframes?


Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Reza RadConsultant, Trainer

This is a sample how your issue might be solved.
Is just tested with local stored files.
You may need to modify the iframeuri if you want to grab external stored content, too.
There is no recursive method to get iframes in iframes, but that should be done with a view lines of code and some reorganisation.

hope that is what you were looking for.

snipped assumes some controls being present.
Download the sample-code.
html-files are included, too to test right away
string url = URL.Text;
            Uri baseUri = new Uri(url);
            webBrowser1.Url = baseUri;
            SourceCode.Text = webBrowser1.DocumentText;

            Regex r = new Regex(@"<iframe (.+)>", RegexOptions.IgnoreCase);
            MatchCollection mCol = r.Matches(SourceCode.Text);

            for (int i = mCol.Count -1; i >= 0; i--)
                Match m = mCol[i];                
                Regex r2 = new Regex("src\\=('|\")(.)+('|\")", RegexOptions.IgnoreCase);
                Match iframeurl = r2.Match(m.Value);
                if (iframeurl.Success)
                    string myUrl = iframeurl.Value.Substring(5, iframeurl.Length - 6);
                    Uri frameuri = new Uri(baseUri, myUrl);
                    WebBrowser wb = new WebBrowser();
                    wb.Url = frameuri;                    
                    SourceCode.Text = SourceCode.Text.Insert(m.Index + m.Length - 9, wb.DocumentText);                    

Open in new window


Sorry for the points award delay

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial