【发布时间】:2015-02-11 06:02:50
【问题描述】:
请参考下面我提供的代码。
****XMLDoc.Active := True;
XMLDoc.Options := [doNodeAutoCreate, doNodeAutoIndent];
XMLDoc.Version := '1.0';
XMLDoc.Encoding := 'utf-8';****
?xml version="1.0" encoding="UTF-8"? 这是我打开 XML 文件时的预期结果。但它没有在 XML 文件中显示 encoding="UTF-8"。因此,我认为度数符号未正确显示在 XML 文件中。(因为它无法编码)。
我将 UTF-8 更改为 UTF-16,但是当我尝试打开 XML 文件时,它显示错误消息,例如 Switch from current encoding to specified encoding not supported。我猜 RAD Studio XE7 中存在一些编码问题,因为对于以前的版本(Delphi XE5)它工作正常。请给我一些建议。
下面我提供示例代码。
XMLDocument1:= TXMLDocument.create(nil);
XMLDocument1.Active := True;
XMLDocument1.Version :='1.0';
XMLDocument1.Encoding :='UTF-8';
XMLDocument2:= TXMLDocument.create(nil);
XMLDocument2.Active := True;
XMLDocument2.Version :='1.0';
XMLDocument2.Encoding :='UTF-8';
{ Add new child. This will become the document element.
If the document element already exists, then an exception is raised. }
//LNode := XMLDocument1.AddChild('Airbus');
LNode := XMLDocument1.CreateElement('TestElement', '101°F (38.33°C)');
XMLDocument1.DocumentElement := LNode;
{ Display document content. }
XMLDocument1.SaveToFile('c:\mk1.xml');
{if we take xmldocuments.xml.text method is not taking “Encoding=’UTF-8’}
XMLDocument2.LoadFromFile ('c:\mk1.xml');
**strsampletext:= XMLDocument2.XML.text; //Here I am not getting "Encoding='UTF-8". and the string "strsampletext" passed as a parameter**. If I write "strsampletext" into another XML file I can't view Degree Symbol correctly when I view in IE.
加载保存的 XML 后,编码的 UTF-8 不取。
【问题讨论】:
-
你能显示你编写 XML 文件的代码吗?
-
欢迎来到 Stack Overflow。不仅在这里,生活中无处不在,一个好的问题描述是这样的:“我做了X。我希望看到Y,但是我得到了Z。”到目前为止,您发布的内容根本不包括X,而且您几乎没有接触过Y 和Z。请edit您的问题提供必要的详细信息。
-
我编辑了问题并提供了必要的详细信息。请给我一些建议。
-
请提供完整的程序。这里没有人愿意花时间尝试重新创建您的程序。请添加一个小的、精简的完整程序。想象一下,我们还有其他事情要做,并且想帮助您,但要在尽可能短的时间内完成。
-
我提供了一个示例代码,希望能帮助您理解问题并找到解决方案。
标签: xml delphi utf-8 delphi-xe7