2

I have this simple class:

public class MappingCollection<T> : List<T>
{
    private int _declaredTotal = -1;
    public int DeclaredTotal { get { return _declaredTotal; } set { _declaredTotal = value; } }
}

And I'm using it inside another class, called "Mapping". Instances of "Mapping" get serialized to xml. Every other property of Mapping gets serialized correctly (classes, scalar properties etc).

When it serializes this custom list that I created, the list elements get serialized correctly, but the "DeclaredTotal" property does not. As it is, it's always serialized as -1, if I remove the default value it's always serialized as 0.

I don't get any runtime error, so I don't really know where the problem resides.

Anyone knows how to solve this strange behavior?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Matteo Mosca
  • 7,380
  • 4
  • 44
  • 80
  • where do you set DeclaredTotal? perhaps you could test it with automatic property – Robert Mar 16 '11 at 09:45
  • Already did. The object has the correct value that I set, both with automatic properties or as I showed above. Still when Serialize is called, the "DeclaredTotal" property does not get serialized. – Matteo Mosca Mar 16 '11 at 09:57
  • what about placing attribute [Serializable] on top of MappingCollection – Robert Mar 16 '11 at 09:58
  • That does not do it either. The class itself gets serialized already. The elements contained (as it is a list) are serialized too. It's just that integer property that does not. – Matteo Mosca Mar 16 '11 at 10:01
  • Could you show us the code how you serialize? – ibram Mar 16 '11 at 10:11
  • possible duplicate of [XmlSerialize a custom collection with an Attribute](http://stackoverflow.com/questions/377486/xmlserialize-a-custom-collection-with-an-attribute) – John Saunders Mar 18 '11 at 06:07

2 Answers2

1

If you find no solution you can try to use a different xml-serializer like sharpserializer or DataContractSerializer

Community
  • 1
  • 1
k3b
  • 14,517
  • 7
  • 53
  • 85
0

Have a look at this similar question.

Looks like you'll have to make the List<T> a property in MappingCollection rather than extending List<T> because of the special way in which collection types are treated by the XmlSerializer.

Community
  • 1
  • 1
Kai G
  • 3,371
  • 3
  • 26
  • 30