title: How to Convert Ansi to UTF 8 with TXMLDocument in Delphi
body: <p>It's possible to convert the XML to UTF-8 encoding in Delphi 6? <br>
  Currently that's what I am doing:</p>
  
  <ul>
  <li>Fill TXMLDocument with AnsiString</li>
  <li>At the end convert the Data to UTF-8 by using <code>WideStringVariable = AnsiToUtf8(Doc.XML.Text);</code>   </li>
  <li>Save the value of <code>WideStringVariable</code> to file using <code>TFileStream</code> and Adding <code>BOM for UTF8</code> at the file beggining.</li>
  </ul>
  
  <p>CODE:</p>
  
  <pre><code>Procedure SaveAsUTF8( const Name:String; Data: TStrings );
  
  const
    cUTF8 = $BFBBEF;
  var
    W_TXT: WideString;
    fs: TFileStream;
    wBOM: Integer;
  begin
    if TRIM(Data.Text) &lt;&gt; '' then begin    
      W_TXT:= AnsiToUTF8(Data.Text);
      fs:= Tfilestream.create( Name, fmCreate );
      try
        wBOM := cUTF8;
        fs.WriteBUffer( wBOM, sizeof(wBOM)-1);
        fs.WriteBuffer( W_TXT[1], Length(W_TXT)*Sizeof( W_TXT[1] ));
      finally
        fs.free
      end;
    end;
  end;
  </code></pre>
  
  <p>If I open the file in Notepad++ or another editor that detects encoding, it shows me UTF-8 with BOM. However, it seems like the text it's not properly encoded. <br></p>
  
  <p>What is wrong and how can I fix it?</p>
  
  <p>UPDATE: XML Properties:</p>
  
  <pre><code>XMLDoc.Version := '1.0';
  XMLDoc.Encoding := 'UTF-8';
  XMLDoc.StandAlone := 'yes';
  </code></pre>