I have the following class structure (not simplified btw):
[Serializable]
[XmlInclude(typeof(Twitter))]
[XmlInclude(typeof(LinkedIn))]
public abstract class SocialNetworkBase : ISocialNetwork
{
public abstract string UserName { get; set; }
}
public class Twitter : SocialNetworkBase
{
public override string UserName { get; set; }
}
public class LinkedIn : SocialNetworkBase
{
public override string UserName { get; set; }
}
And then I am trying to serialize a list of 'SocialNetworkBase' like:
new XmlSerializer(typeof(List<SocialNetworkBase>)).Serialize(ms, socialNetworks)
Which returns
<?xml version="1.0"?>
<ArrayOfSocialNetworkBase xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SocialNetworkBase xsi:type="LinkedIn">
<UserName>someUsername</UserName>
</SocialNetworkBase>
</ArrayOfSocialNetworkBase>
When trying to deserialize using:
return new XmlSerializer(typeof(List<SocialNetworkBase>)).Deserialize(ms) as List<SocialNetworkBase>;
The following error is thrown:
There is an error in XML document (0, 0). ("Root element is missing.")
What is going on here? Deserializing to SocialNetworBase[]
throws the same error.