不知何故,有时下面的代码在加载有效的 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 上也能正常工作。
--杰伦
最佳答案
行为取决于您安装的 MSXML6.DLL
版本。
为了更好地重现这一点,除了问题中的 normal.xml
之外,我还创建了另一个文件 abnormal.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 "."/>..
文件异常.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)
观察:
- 自动 Windows 更新和 Microsoft 更新不会将 MSXML6.DLL 更新到 Windows XP SP3 上的最新版本。
- 没有用于 Windows XP 的 MSXML6 SP3。
- Windows XP SP2 的最新版本实际上是 6.20.2003.0,可以在 http://support.microsoft.com/kb/973686 获得。 (它不会安装在 Windows XP SP3 上)。
- Windows XP SP3 的 6.20.1103.0 版 MSXML6.DLL 可以在 http://support.microsoft.com/kb/973687 获得。 ( direct download for XP SP3 )
- Microsoft 在其网站上几乎没有关于此的信息:http://www.google.com/search?q=msxml6+%226.20.1099.0%22+%226.20.1103.0%22+site:microsoft.com
- 仅搜索 6.20.1103.0 不会显示更多信息:http://www.google.com/search?sourceid=chrome&ie=UTF-8&q=msxml6+%226.20.1103.0%22+site:microsoft.com
- 这提供了最佳搜索结果:http://www.google.com/search?sourceid=chrome&ie=UTF-8&q=msxml6+%226.20.1103.0%22#sclient=psy&num=10&hl=en&q=msxml6+%226.20.1099.0%22+%226.20.1103.0%22
因此:在执行 MSXML6 工作时,首先检查您是否确实拥有适用于目标 Windows 版本的最新 MSXML6.DLL。
--杰伦
关于msxml - 加载有效 Windows-1252 文档时出错 "System error: -2146697210",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4346060/