Xml – What’s a good way to serialize Delphi object tree to XML–using RTTI and not custom code

delphirttiserializationxmlxml-serialization

What's a good way to serialize a Delphi object tree to XML–using RTTI and not custom code?

I would have loved to find that this feature is already built into Delphi, but it doesn't seem to be.

I've found a few components (posted, below) that seem like they might perform this function. Have you used any of them or some other offering? Have you built your own? Am I missing something obvious, in Delphi?

Best Answer

You can use the JVCL TJvAppXMLFileStorage component to serialize TPersistent derived classes.

uses
  JvAppXMLStorage;

var
  Storage: TJvAppXMLFileStorage;
begin
  Storage := TJvAppXMLFileStorage.Create(nil);
  try
    Storage.WritePersistent('', MyObject);
    Storage.Xml.SaveToFile('S:\TestFiles\Test.xml');

    Storage.Xml.LoadFromFile('S:\TestFiles\Test.xml');
    Storage.ReadPersistent('', MyObject);
  finally
    Storage.Free;
  end;
end;
Related Topic