Argad
Argad

Reputation: 45

Delphi: How to prevent MSXML from encoding in UTF-8 an already encoded XML

I was using a stringlist to make a simple xml where I inserted the nodes via Add to make a UTF8 encoded xml

eg.

    myXML.Add('<myNode>'+UTF8Encode('υτφ8στρινγ')+'</myNode>');

However I wanted to switch to IXMLDocument form more clean code and versatility

I am making a simple xml in Delphi

procedure myxml;
var
  xmlDoc : IXMLDocument;
  xNode : IXMLNode;
begin
  xmlDoc := TXMLDocument.Create(nil);
  try
    xmlDoc.Active := TRUE;
    xmlDoc.Version := '1.0';
    xmlDoc.Encoding := 'utf-8';
    xNode := xmlDoc.AddChild('myNode');
    xNode.Text := UTF8Encode('υτφ8στρινγ'); //greek chars
    xmlDoc.XML.SaveToFile('test.xml');  
  finally
    FreeAndNil(xmlDoc);
  end;
end;

The problem is that the output XML is encoded in UTF-8 twice. I believe the problem has to do with MSXML which encodes (via MSXML.save()) in UTF-8 by default.

So what I need is to somehow have MSXML not encode the XML but have the header

    <?xml version="1.0" encoding="utf-8"?>

Upvotes: 2

Views: 2612

Answers (1)

David Heffernan
David Heffernan

Reputation: 612964

The problem is that you should not be performing any UTF-8 encoding. Pass strings to the XML library, and let it do the encoding. So, replace

xNode.Text := UTF8Encode('υτφ8στρινγ');

with

xNode.Text := 'υτφ8στρινγ';

You must remove that call to FreeAndNil. You are passing an interface reference and FreeAndNil is for object instance references. Simply remove it and let the interface reference counting code do its work.

Upvotes: 2

Related Questions