我有遗留代码(不是我写的)始终包含编码属性,但将其重新编译为 D2010,TXMLDocument 不再包含编码。因为 XML 数据在标签和数据上都有重音字符,所以 TXMLDocument.LoadFromFile 只是抛出 EDOMParseErros 说在文件中发现了无效字符。相关代码:
Doc := TXMLDocument.Create(nil);
try
Doc.Active := True;
Doc.Encoding := XMLEncoding;
RootNode := Doc.CreateElement('Test', '');
Doc.DocumentElement := RootNode;
<snip>
//Result := Doc.XMl.Text;
Doc.SaveToXML(Result); // Both lines gives the same result
在旧版本的 Delphi 上,会生成以下行:
<?xml version="1.0" encoding="ISO-8859-1"?>
在 D2010 上,这是生成的:
<?xml version="1.0"?>
如果我手动更改线路,所有工作都像过去几年一样。
更新:XMLEncoding 是一个常量,定义如下
XMLEncoding = 'ISO-8859-1';
最佳答案
您将希望看到 IXMLDocument.CreateProcessingStuction。我用 OmniXML , 但它的语法相似,应该可以帮助您入门:
var
FDoc: IXMLDocument;
PI: IXMLProcessingInstruction;
begin
FDoc := OmniXML.CreateXMLDoc();
PI := FDoc.CreateProcessingInstruction('xml', 'version="1.0" encoding="UTF-8"');
FDoc.AppendChild(PI);
end;
关于xml - 如何使 TXMLDocument(使用 MSXML 实现)始终包含编码属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2759940/