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. The Serialize method is used to serialize an object to XML. This class has been built to serialize any public and published property or Field. This was done to mimic the behavior found in the.NET Xml Serialization with the goal of having a set of objects that can serialize in.NET using Delphi Prism and Win32 using Delphi 2010.
So, I'd like to find an XML serializer for generic Delphi components, similar to the XmlSerializer used in the .NET FW.
The real problem with using binary streaming methods such as Stream's WriteComponent and ReadComponent nominally support similar functionality, it isn't:
- User editable
- Terribly safe when dealing with multiple versions of components
- Very effective at saving the state of nested components (Comp1.Comp2, etc.)
While the XML data binding wizard provides similar functionality it has other drawbacks such as:
Serialize Xml To String
- Forces you to keep an external XML/XSD/etc. file in synch with the code
- Tool-generated, so you can't just edit the code, and have to rely on Adapter classes, or a lot of manual editing
- Also not great for versioning
Generic XML serialization of any object (or TPersistent/TComponent descendant, as is more likely) would be considerably more ideal. I've looked into ObjectBinaryToText as well, but it seems rather fragile and I haven't got it working quite right as of yet. The format is also non-standard; once this is being done, XML makes more sense. I've also poked around in the extensive sourcesoap subdirectory (D6 Enterprise, from work View image: /infopop/emoticons/icon_wink.gif), but haven't found the magic bullet I'm looking for.
If anyone knows of a hidden VCL feature I might be able to adapt, or some set of components which addresses this want of mine, that would be awesome. Thanks in advance View image: /infopop/emoticons/icon_smile.gif
It has been a very lean and easy option of .Net to able serialize/deserialize any serializable object instance.
Delphi Serialize Object To Xml
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
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.
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.