Software Architecture & Engineering Projects for €30 - €250. Hierarchic XML serialization in Delphi XE7 I need a procedure to serialize a delphi complex object to/from a xml file. no custom control/components. using new XML vendor OmniXML. Interface b. XML serialization. Serialization is the process of taking the state of an object and persisting it in some fashion. The.NET Framework includes powerful objects that can serialize any object to XML. The System.Xml.Serialization namespace provides this capability. XML serialization results in strongly typed classes with public properties and fields that are converted to a serial format (in this case, XML) for storage or transport. Because XML is an open standard, the XML stream can be processed by any application, as needed, regardless of platform.
The XmlSerial.pas unit now supports the Attributes. I had to fix a couple of bugs to get it working, just in case you grabbed the copy I posted earlier today.Now you can use 4 Different attributes to control XML serialization process. Specifically XmlRoot, XmlElement, XmlAttribute, and XmlIgnore. I modified the original class I used in the previous XML serialization post to use these.
And now the XML that it outputs and imports is:
So basically this mimics the behavior of the same attributes in the XML .NET Serialization. Although, it does not support namespaces yet.
RTTI Article List
It has been a very lean and easy option of .Net to able serialize/deserialize any serializable object instance.
Only closest option in Delphi is to stream the component using WriteComponent/WriteComponentRes of TStream/TWriter (used for Form storage as DFM, for example). It can be then read back using appropriate counterpart ReadComponent/ReadComponentRes.
Depend on your situation, simply calling .Assign method would work if you are coping data from one instance to another. But it only works between inherited classed which know about each other.
Can we find there something which will allow to pass objects states in more readable format?
There is a very powerful infrastructure available to do full serialization without knowing underlying class structure – RTTI (Run-time Type Information).
All functions we would be looking at are defined in TypInfo.pas unit.
First thing first. To be able to work within RTTI, you need to operate on the object which has publishedand public properties.
A function will return number and reference to the list (array) of properties published (public and published) by the class (VMT information). List will also include published methods. Simply walking through it will give you an access to property/method information.
In our example we are not interested in the published methods, so lets filter it out:
What other Kind of information is present in the list? Bellow is full definition of the type
C# Serialize To Xml
Now you are ready to get or set the value of the property:
And now you are ready to serialize your object, store it, load definition and deserialize an object back. And it could be not necessarily the same object.
You would have to loop through the properties, read the values, and store it as an XML for later use.
Serialize Xml To String
I am not going to go through all specific details in this post, instead, you can find a full code for XML Class serializer here.