开发者

Xml Serialization of a Collection with Properties

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开发者_如何学Go 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?


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜