2
votes

Please see the difference in the degree symbolPlease refer The code I have provided below.

****XMLDoc.Active := True;
  XMLDoc.Options := [doNodeAutoCreate, doNodeAutoIndent];
  XMLDoc.Version := '1.0';
  XMLDoc.Encoding := 'utf-8';****

?xml version="1.0" encoding="UTF-8"? This is the expected result when I open the XML file. But it is not showing encoding="UTF-8" in the XML file. Because of which I think the degree symbol is not properly displayed in the XML file.(as it is not able to encode).

I changed UTF-8 to UTF-16,But when I tried to open the XML file it showed the error message like Switch from current encode to specified encode not supported. I guess there is some encoding problem in RAD Studio XE7 because for previous version ( Delphi XE5) it was working fine. Please give me some suggestions.

Below I am providing the sample code.

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.

After loading the saved XML, the encoded UTF-8 is not taking.

1
Can you show the code where you write the XML file?Uwe Raabe
Welcome to Stack Overflow. Not just here, but everywhere in life, a good problem description goes like this: "I did X. I expected to see Y, but I got Z instead." What you've posted so far doesn't include X at all, and you've only barely touched Y and Z. Please edit your question to include the necessary details.Rob Kennedy
I edited the question and provided the necessary details as well. Please give me some suggestions.Ajita Padhy
Please provide a complete program. Nobody here wants to spend their time trying to re-create your program. Please add a small, cut down complete program. Imagine for a minute that we have other things to do and would like to help you, but do so in as little time as possible.David Heffernan
I have provided a sample code which i hope will help you to understand the issue and find a solution.Ajita Padhy

1 Answers

3
votes

There is nothing wrong here that I can reproduce. Consider this program, based on your code:

{$APPTYPE CONSOLE}

uses
  Xml.XMLIntf, Xml.XMLDoc, Winapi.ActiveX;

procedure Main;
var
  XMLDocument1, XMLDocument2: IXMLDocument;
  LNode: IXMLNode;
begin
  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';
  LNode := XMLDocument1.CreateElement('TestElement', '101°F (38.33°C)');
  XMLDocument1.DocumentElement := LNode;
  XMLDocument1.SaveToFile('c:\desktop\mk1.xml');
  XMLDocument2.LoadFromFile('c:\desktop\mk1.xml');
  Writeln(XMLDocument2.Encoding);
end;

begin
  CoInitialize(nil);
  Main;
  Readln;
end.

The output of this program, when compiled by XE7 update 1, 32 bit, is:

UTF-8

The file that is saved is encoded with UTF-8. This is a screenshot from Notepad++ when I load the file:

enter image description here

Note that the degree symbol is displayed correctly, and has clearly been interpreted as you intend. And note also the encoding reported in the bottom right of the window. Exactly as expected, UTF-8 without a BOM.

You have mis-diagnosed there to be a problem where in fact there is none.


Regarding your update, you seem now to be asking about XML.Text. This is a Delphi string and as such it is always encoded as UTF-16. All Delphi strings are encoded that way. The XML document encoding applies when the document is streamed. So, when you save to a stream or a file, then the encoding comes into play.

If you decide that you wish to save the XML content yourself, behind the back of the XML document object, then you need to take care to use the write encoding. So what is happening to you is that your code, which we still cannot see, is using the wrong encoding. Were you to save XML.Text to a file encoded as UTF-8 then all would be well. The problem is that you are saving with a different encoding. Most likely you are saving using the ANSI encoding.

So perhaps your code runs like this:

Memo1.Lines.Text := XMLDocument1.XML.Text;
Memo1.Lines.SaveToFile(...);

This will fail because the default encoding when saving a TStrings is ANSI.