我正在使用适用于小册子的KML插件,该插件在Google Chrome中效果很好。但是,在IE中,它会在以下代码中引发错误。
<PackageRegistration(UseManagedResourcesOnly:=True)>
<InstalledProductRegistration("#110", "#112", "1.0", IconResourceID:=400)>
<ProvideMenuResource("Menus.ctmenu", 1)>
<Guid(GuidList.guidVSPackage2PkgString)>
Public NotInheritable Class ElektroDocPackage : Inherits Package
#Region " Constructors "
''' <summary>
''' Initializes a new instance of the <see cref="ElektroDocPackage"/> class.
''' </summary>
Public Sub New()
' Inside this method you can place any initialization code that does not require
' any Visual Studio service because at this point the package object is created but
' not sited yet inside Visual Studio environment.
End Sub
#End Region
#Region " Overriden Methods "
''' <summary>
''' Called when the VSPackage is loaded by Visual Studio.
''' This is the place where you can put all the initialization code that rely on services provided by VisualStudio.
''' </summary>
Protected Overrides Sub Initialize()
MyBase.Initialize()
Me.CreateContextMenu()
End Sub
#End Region
#Region " Private Methods "
''' <summary>
''' Creates the context menu.
''' </summary>
Private Sub CreateContextMenu()
Dim applicationObject As DTE2 =
DirectCast(MyBase.GetService(GetType(DTE)), DTE2)
' Get a reference to the context menu of code window.
Dim codeWindowCommandBar As CommandBar =
DirectCast(applicationObject.CommandBars, CommandBars)("Code Window")
' Add a popup command bar.
Dim mainPopup As CommandBarPopup =
DirectCast(codeWindowCommandBar.Controls.Add(MsoControlType.msoControlPopup,
Type.Missing, Type.Missing,
Type.Missing, Type.Missing), CommandBarPopup)
mainPopup.Caption = "ElektroDoc"
' Add controls to the popup command bar.
btCodeExample = DirectCast(mainPopup.Controls.Add(MsoControlType.msoControlButton,
Missing.Value, Missing.Value,
1, True), CommandBarButton)
btCodeExample.Caption = "Code example"
btCodeExample.Style = MsoButtonStyle.msoButtonIcon
End Sub
#End Region
End Class
#End Region
在我看来,这段代码中存在错误 - 作者应该将一个实际的XML字符串传递给parser.parseFromString()函数,而不仅仅是XML文档的url。由于文件的路径不是有效的XML文件(解释:kml文件只是XML),因此解析器会出错是有道理的。但是,这不会导致Chrome调试器工具中出现任何错误,这真的很奇怪。
在我看来,这两种情况都应该失败。 DOMParser上的Trusty MDN文档没有提到将URL作为参数放在parseFromString()中。所以我的问题是为什么这在Chrome中运行,但在IE中出错,然后我该怎么做才能修复它?
请注意,此问题与以下网址不同,因为这不是一般性错误 - 这是关于在Chrome中有效但在IE中失败的内容:Internet Explorer 11 (IE 11) Throws Syntax Error using parseFromString in DOMParser
答案 0 :(得分:3)
当XML格式错误的非Microsoft浏览器(Firefox,Chrome等)时,它将创建带有错误消息的XML文档作为其内容。点击here(&lt; - 点击那里)。
当Microsoft浏览器,IE和Edge中的XML格式错误时,它会抛出错误,将错误写入控制台并停止脚本。请注意我在Mac上,所以我已经远程测试了这个,但没有机会亲自测试它。您可以将该代码放入IE的try catch块中,但我的意思是我不知道是否会阻止它将消息写入控制台。
这里有code pen故意格式错误的XML,错误消息写在输出中。 codepen或输出中没有错误。我故意将解析器中的错误代码写入输出窗口。打开控制台,看看发生了什么。
FWIW IE是正确的行为恕我直言。没有抛出错误是互联网最近做事的方式。没有抛出错误的问题是你不知道你做错了什么或在哪里。写一次,调试一切。
此外,在更新版本之前,IE使用ActiveX来解析XML文档。
来自W3C XML validation脚本:
function validateXML(text) {
var message;
var parser;
var xmlDoc;
// code for Edge, IE, Mozilla, Firefox, Opera, etc.
if (document.implementation.createDocument || window.DOMParser) {
parser = new DOMParser();
try {
xmlDoc = parser.parseFromString(text, "text/xml");
}
catch (error) {
}
if (xmlDoc.getElementsByTagName("parsererror").length > 0) {
return xmlDoc.getElementsByTagName("parsererror")[0];
}
else {
return "No errors found";
}
}
// code for older versions of IE
else if (window.ActiveXObject) {
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = "false";
try {
xmlDoc.loadXML(text);
}
catch (error) {
}
if (xmlDoc.parseError.errorCode != 0) {
message = "Error Code: " + xmlDoc.parseError.errorCode + "\\n";
message = message + "Error Reason: " + xmlDoc.parseError.reason;
message = message + "Error Line: " + xmlDoc.parseError.line;
return message;
}
else {
return "No errors found";
}
}
else {
return "Not supported";
}
}
相关question。