C# WebBrowser控件中模板标记的InnerHtml返回null
我在WinForm表单上有一个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控件中不受支持。我怎样才能修好它?有可能吗 当然,我可以把我的模板放进
WebBrowser
控件,它应该显示一个特定的HTML页面。在HTML页面中,我有一些
应该用来填充页面。当我使用以下代码时:
string template = webBrowser.Document.GetElementById("template-id").InnerHtml
我得到的是null
,而不是实际的内部HTML
我想这是因为模板
标记在旧IE版本中不受支持,因此在WebBrowser
控件中不受支持。我怎样才能修好它?有可能吗
当然,我可以把我的模板放进一个字符串中,然后用它来完成,但是这看起来非常粗糙
谢谢 WebBrowser控件使用Internet Explorer,而Internet Explorer不支持
标记。(见)第节
如果您可以修改页面的内容
如果您可以修改页面内容,则有一些变通方法可供您使用
标记。您可以使用以下任一选项:
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);
};
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窗体浏览器控件基于IE渲染引擎(MSHTML,而不是EdgeHTML),该引擎不支持
元素。如果你想得到支持,你必须使用一个定制的浏览器,比如chromium。此外,它默认为兼容模式IIRC。