Heim > Web-Frontend > js-Tutorial > Hauptteil

Welche Ansätze gibt es, HTML-Code mithilfe von .NET dynamisch zu generieren?

Mary-Kate Olsen
Freigeben: 2024-10-18 08:32:29
Original
816 Leute haben es durchsucht

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>
Nach dem Login kopieren

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>
Nach dem Login kopieren

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>
Nach dem Login kopieren

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

Das obige ist der detaillierte Inhalt vonWelche Ansätze gibt es, HTML-Code mithilfe von .NET dynamisch zu generieren?. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Quelle:php
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Neueste Artikel des Autors
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage