Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WebBrowser控件中模板标记的InnerHtml返回null_C#_Html_.net_Winforms_Webbrowser Control - Fatal编程技术网

C# WebBrowser控件中模板标记的InnerHtml返回null

C# WebBrowser控件中模板标记的InnerHtml返回null,c#,html,.net,winforms,webbrowser-control,C#,Html,.net,Winforms,Webbrowser Control,我在WinForm表单上有一个WebBrowser控件,它应该显示一个特定的HTML页面。在HTML页面中,我有一些应该用来填充页面。当我使用以下代码时: string template = webBrowser.Document.GetElementById("template-id").InnerHtml 我得到的是null,而不是实际的内部HTML 我想这是因为模板标记在旧IE版本中不受支持,因此在WebBrowser控件中不受支持。我怎样才能修好它?有可能吗 当然,我可以把我的模板放进

我在WinForm表单上有一个
WebBrowser
控件,它应该显示一个特定的HTML页面。在HTML页面中,我有一些
应该用来填充页面。当我使用以下代码时:

string template = webBrowser.Document.GetElementById("template-id").InnerHtml
我得到的是
null
,而不是实际的内部HTML

我想这是因为
模板
标记在旧IE版本中不受支持,因此在
WebBrowser
控件中不受支持。我怎样才能修好它?有可能吗

当然,我可以把我的模板放进一个字符串中,然后用它来完成,但是这看起来非常粗糙


谢谢

WebBrowser控件使用Internet Explorer,而Internet Explorer不支持
标记。(见)第节

如果您可以修改页面的内容 如果您可以修改页面内容,则有一些变通方法可供您使用
标记。您可以使用以下任一选项:

  • 选项1-隐藏标签并在边缘模式下加载IE

    webBrowser1.DocumentText = @"
    <html>
    <head>
        <title>Test</title>
        <meta http-equiv=""X-UA-Compatible"" content=""IE=Edge"" />
    </head>
    <body>
        <h1>Test</h1>
        <div>This is a test page.</div>
        <template id=""template1"" style=""display:none;"">
            <h2>Flower</h2>
            <img src=""img_white_flower.jpg"" />
        </template>
    </body>
    </html>";
    webBrowser1.DocumentCompleted += (obj, args) =>
    {
        var element = webBrowser1.Document.GetElementById("template1");
        MessageBox.Show(element?.InnerHtml);
    };
    
    webBrowser1.DocumentText = @"
    <html>
    <head>
        <title>Test</title>
        <!--[if lt IE 9]>
        <script src=""https://cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv.min.js"">
        </script>
        <![endif]-->
    </head>
    <body>
        <h1>Test</h1>
        <div>This is a test page.</div>
        <template id=""template1"">
            <h2>Flower</h2>
            <img src=""img_white_flower.jpg"" />
        </template>
    </body>
    </html>";
    webBrowser1.DocumentCompleted += (obj, args) =>
    {
        var element = webBrowser1.Document.GetElementById("template1");
        MessageBox.Show(element?.InnerHtml);
    };
    
    webBrowser1.DocumentText=@”
    试验
    试验
    这是一个测试页面。
    花
    ";
    webBrowser1.DocumentCompleted+=(对象、参数)=>
    {
    var element=webBrowser1.Document.GetElementById(“template1”);
    MessageBox.Show(元素?.InnerHtml);
    };
    
  • 选项2-包括在页面中

    webBrowser1.DocumentText = @"
    <html>
    <head>
        <title>Test</title>
        <meta http-equiv=""X-UA-Compatible"" content=""IE=Edge"" />
    </head>
    <body>
        <h1>Test</h1>
        <div>This is a test page.</div>
        <template id=""template1"" style=""display:none;"">
            <h2>Flower</h2>
            <img src=""img_white_flower.jpg"" />
        </template>
    </body>
    </html>";
    webBrowser1.DocumentCompleted += (obj, args) =>
    {
        var element = webBrowser1.Document.GetElementById("template1");
        MessageBox.Show(element?.InnerHtml);
    };
    
    webBrowser1.DocumentText = @"
    <html>
    <head>
        <title>Test</title>
        <!--[if lt IE 9]>
        <script src=""https://cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv.min.js"">
        </script>
        <![endif]-->
    </head>
    <body>
        <h1>Test</h1>
        <div>This is a test page.</div>
        <template id=""template1"">
            <h2>Flower</h2>
            <img src=""img_white_flower.jpg"" />
        </template>
    </body>
    </html>";
    webBrowser1.DocumentCompleted += (obj, args) =>
    {
        var element = webBrowser1.Document.GetElementById("template1");
        MessageBox.Show(element?.InnerHtml);
    };
    
    webBrowser1.DocumentText=@”
    试验
    试验
    这是一个测试页面。
    花
    ";
    webBrowser1.DocumentCompleted+=(对象、参数)=>
    {
    var element=webBrowser1.Document.GetElementById(“template1”);
    MessageBox.Show(元素?.InnerHtml);
    };
    
  • 如果无法修改页面的内容 如果无法修改页面内容,则需要使用其他浏览器控件,例如:

  • 你可以用新的。您可以在此处看到要使用的简单步骤:。它将在Windows 10上使用边缘渲染引擎,但对于其他Windows版本,它将再次使用IE
  • 您可以依赖其他浏览器控件,例如

  • windows窗体浏览器控件基于IE渲染引擎(MSHTML,而不是EdgeHTML),该引擎不支持
    元素。如果你想得到支持,你必须使用一个定制的浏览器,比如chromium。此外,它默认为兼容模式IIRC。