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

Posted by Fabricio Araujo on Stack Overflow See other posts from Stack Overflow or by Fabricio Araujo
Published on 2010-05-03T17:32:50Z Indexed on 2010/05/03 17:38 UTC
Read the original article Hit count: 302

I have legacy code (I didn't write it) that always included the encoding attribute, but recompiling it to D2010, TXMLDocument doesn't include the enconding anymore. Because the XML data have accented characters both on tags and data, TXMLDocument.LoadFromFile simply throws EDOMParseErros saying that an invalid character is found on the file. Relevant code:

   Doc := TXMLDocument.Create(nil);  
   try
     Doc.Active := True;
     Doc.Encoding := XMLEncoding;
     RootNode := Doc.CreateElement('Test', '');
     Doc.DocumentElement := RootNode;
     <snip>
     //Result := Doc.XMl.Text;
     Doc.SaveToXML(Result);    // Both lines gives the same result

On older versions of Delphi, the following line is generated:

<?xml version="1.0" encoding="ISO-8859-1"?>

On D2010, this is generated:

<?xml version="1.0"?>

If I change manually the line, all works like always worked in the last years.

UPDATE: XMLEncoding is a constant and is defined as follow

  XMLEncoding = 'ISO-8859-1';

© Stack Overflow or respective owner

Related posts about delphi

Related posts about delphi-2010