Serialization of derived List<T> properties

S

swiftalpha

Aloha,

Smallish problem: I know how to serialize objects and all, but this
has me worried:
Asume this class. Person is a nice simple object.

[Serializable()]
public class PersonList : List<Person>
{
public PersonList()
{}

private int id;
public int ID
{
get { return id; }
set { id = value; }
}
}

If I serialize this class to XML I get a nice ListOfPerson in my xml,
but the PersonList.ID property is nowhere in the xml. Here's the core
of the serialize:

XmlSerializer serializer = new XmlSerializer(inputObject.GetType());
serializer.Serialize(xmlWriter, inputObject);

Anyone?

-Edoode
 
M

Marc Gravell

Well, in general something that has properties *and* is a list causes
problems anyway - for example, you can't data-bind to properties on
something that is an IList (or IListSource), as it assumes you mean "the
current item in the list", not the list itself.

Short of implementing IXmlSerializable, I would suggest having a
container class that has an Id and the list (perhaps "Items", "People"
or similar).

If you need to conform to a specific xml format, then xsd is your friend
- let me know if this is your aim.

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top