Delphi Serialize Object To Xml
Can I serialize a generic list of serializable objects without having to specify their type.
Hey, I want to serialize an object to xml with delphi 7, i searched but i didn't find a full solution, i think with HttpRio we can do it. Thank you in advance. Serialize object delphi 7 Experts Exchange. Tour Start here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site. The Microsoft.NET Framework includes powerful objects that can serialize any object to XML. The System.Xml.Serialization namespace provides this capability. Follow these steps to create a console application that creates an object, and then serializes its state to XML: In Visual C#, create a new Console Application project.
Something like the intention behind the broken code below:
Edit:
For those who wanted to know detail: when I try to run this code, it errors on the XMLSerializer[..] line with:
Cannot serialize interface System.Runtime.Serialization.ISerializable.
If I change to List<object>
I get 'There was an error generating the XML document.'
. The InnerException detail is '{'The type System.Collections.Generic.List1[[Project1.Person, ConsoleFramework, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] may not be used in this context.'}'
Install ulang windows 7 tanpa cd. The person object is defined as follows:
The PersonList is just a List<Person>
.
This is just for testing though, so didn't feel the details were too important. The key is I have one or more different objects, all of which are serializable. I want to serialize them all to one file. I thought the easiest way to do that would be to put them in a generic list and serialize the list in one go. But this doesn't work.
I tried with List<IXmlSerializable>
as well, but that fails with
System.Xml.Serialization.IXmlSerializable cannot be serialized because it does not have a parameterless constructor.
Sorry for the lack of detail, but I am a beginner at this and don't know what detail is required. It would be helpful if people asking for more detail tried to respond in a way that would leave me understanding what details are required, or a basic answer outlining possible directions.
Also thanks to the two answers I've got so far - I could have spent a lot more time reading without getting these ideas. It's amazing how helpful people are on this site.
Dima
9 Answers
I have an solution for a generic List<> with dynamic binded items.
class PersonalList it's the root element
class Person it's an single list element
class SpecialPerson inherits Person
class SuperPerson inherits Person
and the main test Source
Important is the definition and includes of the diffrent types.
slavooSee Introducing XML Serialization:
Items That Can Be Serialized
The following items can be serialized using the XmlSerializer class:
- Public read/write properties and fields of public classes
- Classes that implement
ICollection
orIEnumerable
XmlElement
objectsXmlNode
objectsDataSet
objects
In particular, ISerializable
or the [Serializable]
attribute does not matter.
Now that you've told us what your problem is ('it doesn't work' is not a problem statement), you can get answers to your actual problem, instead of guesses.
When you serialize a collection of a type, but will actually be serializing a collection of instances of derived types, you need to let the serializer know which types you will actually be serializing. This is also true for collections of object
.
You need to use the XmlSerializer(Type,Type[]) constructor to give the list of possible types.
John SaundersJohn SaundersYou can't serialize a collection of objects without specifying the expected types. You must pass the list of expected types to the constructor of XmlSerializer
(the extraTypes
parameter) :
If all the objects of your list inherit from the same class, you can also use the XmlInclude
attribute to specify the expected types :
I think it's best if you use methods with generic arguments, like the following :
Andreas GrechAndreas GrechI think Dreas' approach is ok. An alternative to this however is to have some static helper methods and implement IXmlSerializable on each of your methods e.g an XmlWriter extension method and the XmlReader one to read it back.
If you do go down the route of using the XmlSerializer class directly, create serialization assemblies before hand if possible, as you can take a large performance hit in constructing new XmlSerializers regularly.
For a collection you need something like this:
IanIanSerialize Object To Xml
The easiest way to do it, that I have found. Apply the System.Xml.Serialization.XmlArray
attribute to it.
The serializer will pick up on it being an array and serialize the list's items as child nodes.
Delphi Serialize Object To Xml File
Nate BarbettiniknowTypeList parameter let serialize with DataContractSerializer several known types:
Sharunas BielskisSharunas BielskisIf the XML output requirement can be changed you can always use binary serialization - which is better suited for working with heterogeneous lists of objects. Here's an example:
Use as such:
slavoo