HtmlDocument.Write 剥离引号
由于某种原因,当我尝试写入 HtmlDocument 时,它会删除我给出的字符串的一些(不是全部)引号。
看这里:
HtmlDocument htmlDoc = Webbrowser1.Document.OpenNew(true);
htmlDoc.Write("<HTML><BODY><DIV ID=\"TEST\"></DIV></BODY></HTML>");
string temp = htmlDoc.GetElementsByTagName("HTML")[0].InnerHtml;
temp 的结果是这样的:
<HEAD></HEAD>
<BODY>
<DIV id=TEST></DIV></BODY>
它完全按照它应该的方式工作,只是它去掉了引号。有谁有关于如何预防或解决此问题的解决方案?
For some reason when I try writing to an HtmlDocument it strips some (not all) of the quotation marks of the string I am giving it.
Look here:
HtmlDocument htmlDoc = Webbrowser1.Document.OpenNew(true);
htmlDoc.Write("<HTML><BODY><DIV ID=\"TEST\"></DIV></BODY></HTML>");
string temp = htmlDoc.GetElementsByTagName("HTML")[0].InnerHtml;
The result of temp is this:
<HEAD></HEAD>
<BODY>
<DIV id=TEST></DIV></BODY>
It works exactly as it should except it is stripping the quotation marks. Does anyone have a solution on how to prevent or fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
innerHTML 不能保证它返回的内容与您传入的字符串相同。innerHTML 是由浏览器使用其 HTML 树表示形式构建的 - 因此它将生成它认为合适的结果字符串。
因此,根据您的需要,您可以尝试使用一些 HTML 解析代码来理解不带引号的 ID,或者尝试说服浏览器使用最新的引擎,该引擎更有可能生成您喜欢的 innerHTML。
也就是说,在您的情况下,看起来至少 IE9 将您的 HTML 呈现为 IE9:Quirks 模式(以您不满意的形状返回innerHTML),如果您将有效的 HTML 或强制模式设置为 IE9:Standard,您将得到字符串类似于
IE9:Standards -
"
"
IE9:Quirks -
您可以通过创建示例 HTML 文件并从磁盘打开来亲自尝试。 F12 显示开发工具并在菜单栏中查看模式。
There is no guarantees with innerHTML that it will return content identical to string you passed in. The innerHTML is constructed by browser using its HTML tree representation - so it will produce resulting string as it see fits.
So depending on your needs you can try to use some HTML parsing code that understands ID's without quotes around OR try to convince browser to use latest engine which more likely to produce innerHTML to you liking.
I.e. in your case it looks like at least IE9 renders your HTML as IE9:Quirks mode (that returns innerHTML in the shape your are not happy with), if you make valid HTML or force mode to IE9:Standard you'll get string with qoutes like
IE9:Standards -
"<head></head><body><div id="TEST"></div></body>"
IE9:Quirks -
You can try it yourself by creating sample HTML file and opening from disk. F12 to show dev tools and check out mode in the menu bar.
C# 有一个奇怪的功能,虽然我不确定它的名字。抱歉,我不确定 VB 是否有等效项。
在文字字符串的开头添加
@
以转义所有字符。另外,这并不重要,但您的 html 不会验证。所有标签和属性都应该小写。例如
应该是
。
C# has a quirky feature though I'm not sure of it's name. Sorry i'm not sure of a vb equivalent.
Add an
@
at the beginning of a literal string to escape all characters.Also, this isn't important but your html would not validate. All tags and attributes should be lower case. E.g.
<HTML>
should be<html>
.