Home > Web Front-end > JS Tutorial > body text

What are the Approaches to Dynamically Generate HTML Code Using .NET?

Mary-Kate Olsen
Release: 2024-10-18 08:32:29
Original
726 people have browsed it

What are the Approaches to Dynamically Generate HTML Code Using .NET?

How to Generate HTML Code Dynamically Using .NET and WebBrowser or mshtml.HTMLDocument

Introduction

Dynamically generating HTML code using .NET provides flexibility and control over web page content. This article explores two approaches for retrieving dynamic HTML code: utilizing the WebBrowser class from System.Windows.Forms and leveraging the COM interface mshtml.HTMLDocument from the Microsoft HTML Object Library assembly.

Approach 1: Utilizing WebBrowser

The WebBrowser class is a convenient option for loading web pages and accessing their content. The following code demonstrates how to load a page and retrieve its HTML using the WebBrowser DocumentCompleted event:

<code class="csharp">WebBrowser wb = new WebBrowser();
wb.Navigate("https://www.google.com/#q=where+am+i");

wb.DocumentCompleted += delegate(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    mshtml.IHTMLDocument2 doc = (mshtml.IHTMLDocument2)wb.Document.DomDocument;
    foreach (IHTMLElement element in doc.all)
    {
        System.Diagnostics.Debug.WriteLine(element.outerHTML);
    }     
};</code>
Copy after login

Approach 2: Utilizing mshtml.HTMLDocument

The mshtml.HTMLDocument interface offers a direct way to interact with HTML documents. You can use it to load and access HTML from a string:

<code class="csharp">mshtml.IHTMLDocument2 doc = (mshtml.IHTMLDocument2)new mshtml.HTMLDocument();
doc.write(new System.Net.WebClient().DownloadString("https://www.google.com/#q=where+am+i"));

foreach (IHTMLElement e in doc.all)
{
    System.Diagnostics.Debug.WriteLine(e.outerHTML);
}</code>
Copy after login

Limitations of WebBrowser and mshtml.HTMLDocument

Both the WebBrowser and mshtml.HTMLDocument approaches may not always return the fully rendered HTML code. To address this, an improved approach using async/await and cancellation tokens is provided as an enhanced response in the reference content. This approach monitors HTML changes dynamically and retrieves the content when it is fully rendered.

Code Sample

The following optimized code demonstrates the improved approach:

<code class="csharp">using Microsoft.Win32;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WbFetchPage
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            SetFeatureBrowserEmulation();
            InitializeComponent();
            this.Load += MainForm_Load;
        }

        async void MainForm_Load(object sender, EventArgs e)
        {
            try
            {
                var cts = new CancellationTokenSource(10000); // cancel in 10s
                var html = await LoadDynamicPage("https://www.google.com/#q=where+am+i", cts.Token);
                MessageBox.Show(html.Substring(0, 1024) + "..." ); // it's too long!
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        async Task<string> LoadDynamicPage(string url, CancellationToken token)
        {
            var tcs = new TaskCompletionSource<bool>();
            WebBrowserDocumentCompletedEventHandler handler = (s, arg) => tcs.TrySetResult(true);

            using (token.Register(() => tcs.TrySetCanceled(), useSynchronizationContext: true))
            {
                this.webBrowser.DocumentCompleted += handler;
                try 
                {           
                    this.webBrowser.Navigate(url);
                    await tcs.Task; // wait for DocumentCompleted
                }
                finally
                {
                    this.webBrowser.DocumentCompleted -= handler;
                }
            }

            var documentElement = this.webBrowser.Document.GetElementsByTagName("html")[0];

            var html = documentElement.OuterHtml;
            while (true)
            {
                await Task.Delay(500, token); 

                if (this.webBrowser.IsBusy)
                    continue; 

                var htmlNow = documentElement.OuterHtml;
                if (html == htmlNow)
                    break; 

                html = htmlNow;
            }

            token.ThrowIfCancellationRequested();
            return html;
        }

        static void SetFeatureBrowserEmulation()
        {
            if (LicenseManager.UsageMode != LicenseUsageMode.Runtime)
                return;
            var appName = System.IO.Path.GetFileName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);
            Registry.SetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION",
                appName, 10000, RegistryValueKind.DWord);
        }
    }
}</code>
Copy after login

This code ensures that the page is fully rendered before retrieving the HTML content, resulting in more accurate and reliable results.

The above is the detailed content of What are the Approaches to Dynamically Generate HTML Code Using .NET?. For more information, please follow other related articles on the PHP Chinese website!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!