使用XmlSerializer序列化dervied类成员

时间:2016-04-15 15:43:42

标签: c# xml serialization xmlserializer

我有一个派生自可序列化类的类

[Serializable]
[XmlRoot("AccountPersonalizationData")]
public class AccountPersonalizationData : LookupKeyedCollection<Type, PersonalizedEntityState>
{
    [XmlElement(ElementName="SavedProfiles")]
    public string SavedProfiles { get; set; }

    public AccountPersonalizationData() : base(GetKeyFromPersonalizedEntityState)
    {
    }

    private static Type GetKeyFromPersonalizedEntityState(PersonalizedEntityState item)
    {
        return item.UnderlyingType;
    }

}

LookupKeyedCollection派生自KeyedCollection

[XmlInclude(typeof(AccountPersonalizationData))]
public class LookupKeyedCollection<TKey, TItem> : KeyedCollection<TKey, TItem>
{
...
}

当我尝试序列化某些东西时,只有LookupKeyedCollection被序列化,而SavedProfiles属性却没有。

XmlSerializer serializer = new XmlSerializer(typeof(AccountPersonalizationData));
serializer.Serialize(xmlWriter, persData);

0 个答案:

没有答案