請參閱下面提供的代碼。使用Delphi XE7中的TXML Document對象在XML文件中無法正確顯示度符號
****XMLDoc.Active := True;
XMLDoc.Options := [doNodeAutoCreate, doNodeAutoIndent];
XMLDoc.Version := '1.0';
XMLDoc.Encoding := 'utf-8';****
?xml version =「1.0」encoding =「UTF-8」?這是預計結果當我打開XML文件。但是它沒有在XML文件中顯示編碼=「UTF-8」。正因爲如此,我認爲度數符號沒有正確顯示在XML文件中(因爲它無法進行編碼)。
我將UTF-8更改爲UTF-16,但是當我試圖打開XML文件時,它顯示了錯誤消息,如從當前編碼切換到指定編碼不支持。我猜在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文件的位置嗎? – 2015-02-11 07:10:59
歡迎來到Stack Overflow。不只是在這裏,而是在生活中的任何地方,一個好的問題描述是這樣的:「我做了* X *,我期望看到* Y *,但是我得到了* Z *。」到目前爲止,你發佈的內容根本不包括* X *,而你幾乎沒有碰到* Y *和* Z *。請[編輯]您的問題以包括必要的細節。 – 2015-02-11 08:00:42
我編輯了問題並提供了必要的細節。請給我一些建議。 – 2015-02-11 09:31:04