Search code examples
pascallazarusfreepascal

Lazarus open XML file in TMemo


I'm trying to open an XML file in a TMemo as text. Everytime I do this is what it opens:

enter image description here

I know that's not what's in the XML, if I open that file in Notepad it opens fine and shows XML data, plain text.

This is my code:

procedure TForm1.Button7Click(Sender: TObject);
var
  ss: string;
  sl: TStringList;
begin
  ss := '';
  runcommand('msinfo32 /nfo pcinfo.xml', ss);
  sl:=TStringList.Create;
  sl.LoadFromFile('pcinfo.xml');
  Memo2.Text := sl.Text;
  sl.Free;
end;

Solution

  • The file was saved in Unicode, opening Notepad and Save As the file showed me encoding. So opening the file into a stream, then converting to UTF8 worked like a charm.

    stream := TMemoryStream.Create;
      try
        stream.LoadFromFile('pcinfo.xml');
        SetLength(s, stream.Size);
        stream.ReadBuffer(s[1], stream.Size);
        memo2.Text := ConvertEncoding(s, GuessEncoding(s), EncodingUTF8);
      finally
        stream.Free;
      end;