I tried in several ways to get XmlSerializer.Deserialize to return null however this doesn't seem possible
I tried with a class that was null, malformated xml, well formatted xml.
I may not be something obvious here, but is it possible?
Just to clarify, give the MyClass class, which is serializable, I want a similar test to be as follows:
[Fact]
public void When_xml_Something_Then_serialize_returns_null()
{
string serializedObject = "<?xml version=\"1.0\" encoding=\"utf-8\"?><MyClass xmlns:xsi=\"http://www.w3asdsadasdasd.org/2001/XMLSchema-instance\"></MyClass>";
using (var stringReader = new StringReader(serializedObject))
{
Assert.Null(new XmlSerializer(typeof(MyClass)).Deserialize(stringReader));
}
}
I tried different things in a serialized string, and I either get an exception or an empty instance of MyClass :( thanks to NOTE. There was a typo in this question, now it is fixed.
NOTE 2: see comments for a more detailed answer.