Q:

.NET deserialization - multiple types are returned

We have a framework built up whose API is this: 
T Deserialize<T>(string to deserialize)
{
    //  XmlSerializer(typeof(T))
    //  deserialize and return
}

We've been passing it things like: 
[XmlRoot("apple")]
public class Apple
{
    [XmlElement("Id")]
    public int AppleId { get; set; }
}

this works great when we are returned an apple, however, sometimes we are getting "notAnApple".  So I will either get "apple" or "notAnApple".  When I get "notAnApple", I get an exception.
As we have a lot of code built around this method, I would prefer not to go around changing things like the method signature.  I do, however, have full flexibility into passing whatever class I like into this deserialization method.

A:

Generally you "know" exactly what you're serializing and deserializing.  e.g. you may serialize like this:
serializer.Serialize(stream, new Apple());
serializer.Serialize(stream, new notAnApple());

Then you'd serialize like:
Apple apple = (Apple)deserialzer.Deserialize(stream);
notAnApple peach = (notAnApple)desserializer.Deserialize(stream);

You don't normally randomly try to deserialize data to figure out what it is that you're deserializing.  Technically you already know because you have a specific file format or a specific stream format.

