msxml - 加载有效 Windows-1252 文档时出错 "System error: -2146697210"

标签 msxml msxml6

不知何故,有时下面的代码在加载有效的 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="à"/>

我期望的行为是:

  1. abnormal.xml 失败,因为它没有指定编码,但包含一个设置了高位的字符
  2. 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。

--杰伦

关于msxml - 加载有效 Windows-1252 文档时出错 "System error: -2146697210",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4346060/

相关文章:

xml - 根据 XSD 中的属性值使 XML 元素成为必需

xml - 无法解析服务器名称或地址,为什么?

xml - EOutOfMemory 使用 Delphi 创建大型 XML

xml - 动态生成 XSD 时 XML 架构中根元素的定义不正确

c++ - MSXML 内存泄漏

http - MSXML6.dll Access Denied 将 HTTP 重定向到 HTTPS

javascript - 我应该在我的 WIN7 IE9 机器上从 MSXML4 升级还是降级

soap - 使用经典 ASP 发送 'application/soap+xml' SOAP 请求

Windows Server 2008 上的 Paypal 经典 asp msxml 错误

c++ - MSXML XSLT 解析器版本