c# inheriting generic collection and serialization...
Posted
by Stecy
on Stack Overflow
See other posts from Stack Overflow
or by Stecy
Published on 2009-03-20T13:06:59Z
Indexed on
2010/04/29
9:07 UTC
Read the original article
Hit count: 358
Hi,
The setup:
class Item
{
private int _value;
public Item()
{
_value = 0;
}
public int Value { get { return _value; } set { _value = value; } }
}
class ItemCollection : Collection<Item>
{
private string _name;
public ItemCollection()
{
_name = string.Empty;
}
public string Name { get {return _name;} set {_name = value;} }
}
Now, trying to serialize using the following code fragment:
ItemCollection items = new ItemCollection();
...
XmlSerializer serializer = new XmlSerializer(typeof(ItemCollection));
using (FileStream f = File.Create(fileName))
serializer.Serialize(f, items);
Upon looking at the resulting XML I see that the ItemCollection.Name value is not there!
I think what may be happening is that the serializer sees the ItemCollection type as a simple Collection thus ignoring any other added properties...
Is there anyone having encountered such a problem and found a solution?
Regards,
Stécy
© Stack Overflow or respective owner