Rumah > hujung hadapan web > tutorial js > Apakah Pendekatan Menjana Kod HTML Secara Dinamik Menggunakan .NET?

Apakah Pendekatan Menjana Kod HTML Secara Dinamik Menggunakan .NET?

Mary-Kate Olsen
Lepaskan: 2024-10-18 08:32:29
asal
823 orang telah melayarinya

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>
Salin selepas log masuk

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>
Salin selepas log masuk

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>
Salin selepas log masuk

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

Atas ialah kandungan terperinci Apakah Pendekatan Menjana Kod HTML Secara Dinamik Menggunakan .NET?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

sumber:php
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel terbaru oleh pengarang
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan