20.08.2019

Delphi Serialize Object To Xml

Delphi Serialize Object To Xml Rating: 3,6/5 5947 reviews
  1. Serialize Object To Xml
  2. Delphi Serialize Object To Xml File
  3. Delphi Serialize Object To Xml Codes
Active1 year, 6 months ago

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
6,3144 gold badges19 silver badges40 bronze badges
Simon DSimon D
2,6115 gold badges30 silver badges43 bronze badges
Object

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.

slavoo
4,11612 gold badges30 silver badges36 bronze badges
DamaschDamasch

See 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 or IEnumerable
  • XmlElement objects
  • XmlNode objects
  • DataSet 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 Saunders
149k23 gold badges209 silver badges371 bronze badges

You 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 :

Thomas LevesqueThomas Levesque
243k57 gold badges519 silver badges688 bronze badges

I think it's best if you use methods with generic arguments, like the following :

Andreas GrechAndreas Grech
64.7k93 gold badges278 silver badges352 bronze badges

I 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:

IanIan
24.8k20 gold badges84 silver badges158 bronze badges
SwDevMan81
39.9k19 gold badges130 silver badges167 bronze badges
ligaorenligaoren

Serialize Object To Xml

5731 gold badge6 silver badges10 bronze badges

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 Barbettini
31.4k17 gold badges93 silver badges122 bronze badges
LeeLee

knowTypeList parameter let serialize with DataContractSerializer several known types:

Sharunas BielskisSharunas Bielskis
5731 gold badge11 silver badges15 bronze badges

If 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
4,11612 gold badges30 silver badges36 bronze badges

Delphi Serialize Object To Xml Codes

Robert VenablesRobert Venables
5,6931 gold badge19 silver badges35 bronze badges

Not the answer you're looking for? Browse other questions tagged c#listgenericsxml-serialization or ask your own question.