durumdara
durumdara

Reputation: 3483

Why does TXmlDocument omit the encoding when I serialize to a string?

If I build an XML from line to line, I can set the encoding, but when I load it from file, I cannot add the encoding. See:

procedure TForm1.Button1Click(Sender: TObject);
var
    x : TXMLDocument;
    s : string;
    w : WIdeString;
begin
    s := '<?xml version="1.0"?><a><b/></a>';
    x := TXMLDocument.Create(Self);
    x.XML.Text := s;
    x.Active := True;
    x.Encoding := 'UTF-8';
    x.DocumentElement.childNodes['b'].attributes['x'] := '1';
    x.SaveToXML(w);
    ShowMessage(w);
end;

Interesting that the "encoding" part is missing from the result!

How do I make the result contain the XML encoding?

Upvotes: 2

Views: 866

Answers (1)

Martijn
Martijn

Reputation: 13632

You’re saving your XML to a WideString. A WideString is, by definition, UTF16-encoded, so there’s no point whatsoever in specifying another encoding. IIRC, you can specify an encoding="UTF-16", which won’t be removed, since that’s what it is.

You can, however, specify a different encoding if you consequently save the XML document to a stream.

Upvotes: 4

Related Questions