Is there any example to serialize/deserialize a TROArray descendant to an XML file ?
Hello,
If you just need to write content of TROArray descedent in XML file you could simply write in file a usual in delphi:
var ROArray:TROIntArray;
begin
âŚ
WriteLn(F, ROArray.ContentAsXml);
âŚ
end;
Best regards
And to read back from xml ?
Hello,
Sorry for not full answer.
If XML of TROArray is get from ContentAsXml method, then for applying this XML back to the TROArray you should write some syntax parser and sequently add values of tags to the array.
So it is more convenient to use TROXMLSerializer.
var
ROArray:TROIntArray;
doc1,doc2:IXMLDocument;
lxml:TROXMLSerializer;
begin
âŚ
// Serializing
doc1:=NewROXmlDocument;
doc1.New(ROArray.ClassName);
lxml := TROXMLSerializer.Create(pointer(doc1.DocumentNode));
try
lxml.Write(ROArray.ClassName, ROArray.ClassInfo, ROArray);
finally
lxml.Free;
end;
doc1.SaveToFile(âarray.xmlâ);
âŚ
//Deserializing
doc2:=NewROXmlDocument;
doc2.New;
doc2.LoadFromFile(âarray.xmlâ);
lxml := TROXMLSerializer.Create(pointer(doc2.DocumentNode));
try
lxml.Read(ROArray.ClassName, ROArray.ClassInfo, ROArray);
finally
lxml.Free;
end;
âŚ
end;
Best regards
It works! Is there any option to make the generated XML more human-readable i.e. with newlines and indentation ?
xmlDoc.FormatXMLData(SomeXMLString)
Hello,
Iâm trying to deserialize XML which I have serialized throug ContentAsXML. I am using the following code:
DC := TMyRODataObject.Create;
MemoryStream := TStringStream.Create(XMLDocumentAsString);
try
XMLDocument := NewROXmlDocument;
XMLDocument.New;
XMLDocument.LoadFromStream(MemoryStream);
XMLSerializer := TROXMLSerializer.Create(Pointer(XMLDocument.DocumentNode));
try
XMLSerializer.Read(DC.ClassName, DC.ClassInfo, DC);
Result := DC;
finally
XMLSerializer.Free;
end;
finally
MemoryStream.Free;
end;
My DC object is always nil. The object is a TROComplexType object. All properties are published and some of them are also TROCompleyType.
How do I the deserialization? Could you help me?
Thank you!
Best regards!
There is no need to create a TROXMLSerializer instance manually.
You can use the XmlToObject and ObjectToXml functions from uROXmlSerializer.pas.
XmlToObject returns a TROComplexType object which must be casted into the current target type.
Thanks! But it doesnât work for me! Iâve got an error âunknown class structâ
The XML which I am trying to convert comes from â.ContentAsXmlâ
I am using the following code:
TMyDataObject(XMLToObject(ContentAsXml));
Iâd assume that ContentAsXml generates a different XML code (where the root node isnât the original object type name)
Please run XmlToObject and ContentAsXml and compare the XML codes. I have no Delphi on my PC.
This example works fine, thanks for that.
I use it to save TROArray descendants in a database.
Is there also a JSON variant available? XMLâs overhead is huge.
Kind regards,
Michel
Hi,
we support Variant value in JsonMessage.
this code:
procedure TClientForm.Button1Click(Sender: TObject);
var
k: IROMessage;
str: TROBinaryMemoryStream;
larray: NewArray;
begin
str:= TROBinaryMemoryStream.Create();
larray:= NewArray.Create;
lArray.Add('some string');
lArray.Add(Now);
lArray.Add(True);
lArray.Add(3456);
lArray.Add(1234.0987);
lArray.Add(Null);
k := Message as IROMessage;
k.Initialize(nil,'','',mtResponse);
try
k.WriteArray('', larray);
k.WriteToStream(str);
Memo1.Lines.LoadFromStream(str);
finally
k := nil;
str.Free;
larray.Free;
end;
end;
will generate
{âversionâ:â1.1â,âresultâ:[âsome stringâ,â2020-02-19T11:35:09.932â,true,3456,1234.0987,null]}