How to make TXMLDocument (with the MSXML Implementation) always include the encoding attribute? How to make TXMLDocument (with the MSXML Implementation) always include the encoding attribute? xml xml

How to make TXMLDocument (with the MSXML Implementation) always include the encoding attribute?


You'll want to see IXMLDocument.CreateProcessingStruction. I use OmniXML, but it's syntax is similar and should get you started:

var  FDoc: IXMLDocument;  PI:  IXMLProcessingInstruction;begin  FDoc := OmniXML.CreateXMLDoc();  PI := FDoc.CreateProcessingInstruction('xml', 'version="1.0" encoding="UTF-8"');  FDoc.AppendChild(PI);end;


var   XMLStream: TStringStream;begin     Doc := TXMLDocument.Create(nil);     try     Doc.Active := True;     Doc.Encoding := XMLEncoding;     RootNode := Doc.CreateElement('Test', '');     Doc.DocumentElement := RootNode;     <snip>     XMLStream := TStringStream.Create;     Doc.SaveToStream(XMLStream);     Result := XmlStream.DataString;     XMLStream.Free;

Since Ken's answer and the link to MSXML article, I decided to investigate the XML property and SaveToXML method. Both use the XML property of the MSXMLDOM implementation - which in the article is said that do not bring the encoding when directly read ( in the "Creating New XML Documents with MSXML" section right after the use of CreateProcessInstruction method).

UPDATE:

I found that accented characters are getting truncated in the resulting XML. When the processor of that XML started to throw strange errors, we saw that the chars are being converted to the numeric char constant ( #13 is the numeric char constant for carriage return). So, I used a TStringStream to get it FINALLY right.