不知何故,有时下面的代码在加载有效的Windows-1252 XML时会产生错误。
使用MSXML6在Windows XP Professional x86 SP3上失败 它使用MSXML6在Windows 7 Ultimate x64 SP1上成功。
注意:下面的代码是用Delphi编写的,但是在其他环境中,等效代码也会失败。
procedure TXMLEOSErrorTestCase.Test;
var
XmlDocument: IXMLDOMDocument3;
XmlFileName: string;
begin
XmlDocument := CoFreeThreadedDOMDocument60.Create();
XmlFileName := TPath.Combine(TPath.GetDirectoryName(ParamStr(0)), '1-Normal.xml');
if not XmlDocument.load(XmlFileName) then
Parse(XmlDocument.parseError);
end;
在XmlDocument.load方法期间发生此错误:
reason: System error: -2146697210.
errorCode: -2146697210
url: C:\temp\1-Normal.xml
我将XML修剪为下面的XML。
这是XML文件的十六进制转储:
000000: 3C 3F 78 6D 6C 20 76 65 72 73 69 6F 6E 20 3D 20 <?xml version =
000010: 22 31 2E 30 22 20 65 6E 63 6F 64 69 6E 67 3D 22 "1.0" encoding="
000020: 57 69 6E 64 6F 77 73 2D 31 32 35 32 22 3F 3E 3C Windows-1252"?><
000030: 52 4F 57 20 43 69 74 79 3D 22 E0 22 2F 3E 0D 0A ROW City="."/>..
这是XML:
<?xml version = "1.0" encoding="Windows-1252"?><ROW City="à"/>
为什么会出现错误?
(XML在.NET和其他不使用MSXML6的环境中加载完全正常,它在Windows 7 Ultimate x64 SP1上也能正常工作。)
- 的Jeroen
答案 0 :(得分:6)
行为取决于您安装的MSXML6.DLL
版本。
为了更好地重现这一点,除了问题中的abnormal.xml
之外,我还创建了另一个文件normal.xml
。
文件转储abnormal.xml
:
000000: 3C 3F 78 6D 6C 20 76 65 72 73 69 6F 6E 3D 22 31 <?xml version="1
000010: 2E 30 22 20 73 74 61 6E 64 61 6C 6F 6E 65 3D 22 .0" standalone="
000020: 79 65 73 22 3F 3E 3C 52 4F 57 20 43 69 74 79 3D yes"?><ROW City=
000030: 22 E0 22 2F 3E 0D 0A "."/>..
档案abnormal.xml
:
<?xml version="1.0" standalone="yes"?><ROW City="à"/>
文件转储normal.xml
:
000000: 3C 3F 78 6D 6C 20 76 65 72 73 69 6F 6E 20 3D 20 <?xml version =
000010: 22 31 2E 30 22 20 65 6E 63 6F 64 69 6E 67 3D 22 "1.0" encoding="
000020: 57 69 6E 64 6F 77 73 2D 31 32 35 32 22 3F 3E 3C Windows-1252"?><
000030: 52 4F 57 20 43 69 74 79 3D 22 E0 22 2F 3E 0D 0A ROW City="."/>..
档案normal.xml
:
<?xml version = "1.0" encoding="Windows-1252"?><ROW City="à"/>
我期望的行为是:
abnormal.xml
失败,因为它没有指定编码,但包含一个带有高位集的字符normal.xml
成功,因为它支持支持高位字符的单字节编码,因此允许使用高位设置的字符以下是观察到的情景:
MSXML6失败:
reason: System error: -2146697210.
errorCode: -2146697210
url: file:///C:/My%20Dropbox/XMLEOSErrorTest/Abnormal.xml
reason: System error: -2146697210.
errorCode: -2146697210
url: file:///C:/My%20Dropbox/XMLEOSErrorTest/Normal.xml
MSXML6成功:
reason: An invalid character was found in text content.
errorCode: -1072896760
url: file:///C:/My%20Dropbox/XMLEOSErrorTest/Abnormal.xml
srcText: <?xml version="1.0" standalone="yes"?><ROW City="
line: 1
linepos: 50
filepos: 49
这是对哪些版本失败的概述 括号中DLL的名称来自它们的版本信息。
failure; XP Professional SP3:
msxml6.dll version 6.20.1099.0 (MSXML 6.0 SP2)
msxml6r.dll version 6.0.3883.0 (XML Resources)
success; Windows 7 Ultimate x64 SP1:
msxml6.dll version 6.30.7600.16385 (MSXML 6.0 SP3)
msxml6r.dll version 6.30.7600.16385
msxml6r.dll.mui version 6.30.7600.16385
success; XP Professional SP3:
msxml6.dll version 6.20.1103.0 (MSXML 6.0 SP3)
msxml6r.dll version 6.0.3883.0 (XML Resources)
观察:
所以:在进行MSXML6工作时,首先检查一下你确实拥有适用于目标Windows版本的最新MSXML6.DLL。
- 的Jeroen