This step-by-step article describes how to serialize an object to XML by using Visual Basic.NET or Visual Basic 2005. This method is useful for persisting an object's state. This method is also useful for cloning an object by de-serializing the XML back to a new object.
JVCL is one choice, but if you prefer a small, self-contained library, there's OmniXML (Mozilla Public License 1.1, ). I've used it successfully in several projects, and I find it the simplest XML library to use in Delphi. OmniXML comes with 'OmniXMLPersistent' unit, which does what you need via RTTI, just like the JVCL solution does. // saving: pers: TPersistent; // SaveToFile is a class method, so no need to instantiate the object: TOmniXMLWriter.SaveToFile( pers, 'd: path file.xml', pfAttributes, ofIndent ); pfAttributes means properties will be stored as attributes of XML elements; ofIndent will produce a nicely indented code for readability.
// loading: TOmniXMLWriter.LoadFromFile( pers, 'd: path file.xml' ).
Quote The XML mapper does not serialize the ECO objects. It serialises an internal representation of their contents. Even if the objects where serializable, that would be on no use whatsoever, since the serialisation is done on the fields of the object, while what you want is the values of the properties. You could try with the XMLSerializer class. However, its serialisation is controlled by attributes on the classes, and for your needs I really think that you will want to serialise the same objects in different ways. You are in effect converting part of a network (the objects and their relationships) into a tree. As an example, in one case you may want a list of persons with just their attributes, in another you want the same list, but with the relationship to the department they belong to expanded so that the department (or part of it, say the name) is included as subnodes for the person.
![Delphi serialize object to xml code Delphi serialize object to xml code](http://images.cnitblog.com/i/8867/201404/162136539942542.png)
I think the best approach is to build a simple meta-information driven serialiser, with a few virtual methods (or events if you want it to de a component) to specify the actions when following relationships. I was sort of thinking I could just serialize an object, get all of the properties into a doc, then use XSL to just grab the ones that I wanted.
Instead, I have used XMLTextWriter and methods for each class / node, passing objects and the doc into each. I can control the actual attribuutes added to the doc and the size of the doc. I'm just writing some XSL now to format the XML. I'm using this approach for creating printed receipts, as an alternative to use a report writer and SQL. I really hate XSL, I've been looking for a WYSIWYG editor. There is scope for a wizard for generating XML from an object, where the developer checks which associations and which properties of those associations to output.
Project for some other time. In this case I passed a 'Hire' object (without any payments), which produced: Inski Newcastle 003 037 205 All Prices Include GST. Please Read terms & conditions, return all gear on time, in good clean condtion.
Chains must be tried on your car before leaving for the snow. Cnr King & National Park Streets Newcastle West NSW 23 5177 Phillps 10000 2/12/2004 12:00:00 AM 2/12/2004 1:03:03 AM 4/12/2004 1:02:45 AM $16.00 $70.00 dick Walker 10 Lisa Phillps 10005 $10.00 $50.00 Poles 105 $6.00 $6.00 $30.00 Wrist Guard S $4.00 $4.00 $20.00 Tom Walker 10006 $6.00 $20.00 Tobbogan $6.00 $6.00 $20.00.