views:

66

answers:

2

I ve been trying in a few different ways to get XmlSerializer.Deserialize to return null however it doesnt seem possible

I tried with a class being null, malformated xml, well formatted xml .

I might be missing something obvious here, but is it possible ?

Just to clarify give a Class MyClass that is serializable I want a similar test to the following to pass

[Fact] //this is a the test attribute when using xUnit
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\"&gt;&lt;/MyClass&gt;";
    using (var stringReader = new StringReader(serializedObject))
    {
        Assert.Null(new XmlSerializer(typeof(MyClass)).Deserialize(stringReader));
    }
}

Tried different things in the serialized string, and i either get an exception or an empty instance of MyClass :( Thanks NOTE: there was a typo in this question, it is now corrected

NOTE 2: for a more detailed answer look at the comments.

+2  A: 

Yes, Deserialize can return null when the input does not contain XML that is unexpected. This is frequently seen when there is confusion of the XML namespaces. If the input contains a root element with the expected name, but in a different namespace, then null will be returned.

This is often seen when dealing with ASMX web services or with Web References, especially web references against RPC-style services, where the messages are described in terms of the XSD type of the message, and not in terms of the element.

John Saunders
you are right ( my typo), corrected the question
Miau
tried a few different things on the namespace and the test above is failing, any clues?
Miau
Put `[XmlRoot(Namespace="http://example.org")]` on `MyClass` and try again.
John Saunders
Added that and if i change the xmlns= from example to anything else it throws, with bad formated exception. I think it must be along the lines tho so I m accepting your answer as valid
Miau
Can you show the XML element that contained the `xmlns`? I'm concerned about it throwing an exception.
John Saunders
added the code here http://gist.github.com/589424#comments
Miau
@Miau: you should try using the `StringReader` to load an `XmlDocument`, in order to see if there might not actually be an error in the XML document. Then post the complete exception, including any InnerException instances.
John Saunders
not sure I follow
Miau
@Miau: I mean, try `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)) {XmlDocument doc = new XmlDocument(); doc.Load(reader);}`. Then post the full exception if you get one.
John Saunders
Tried that (code in the gist, second test) http://gist.github.com/589424, two exceptions happen: an XmlException and System.InvalidOperationException: There is an error in XML document (0, 0). that exception happen ins the deserailization line tho (ie in the line after doc.Load(stringReader);)
Miau
@Miau: so the fact is, there _is_ an error in the XML.
John Saunders
well, but thats the whole point of the question -> I can't get the Deserializer to be null it either throws or i get back just a newed up instance. I m trying to find the instance where I get a back null
Miau
@Miau: you certainly won't get null back from invalid XML. I'm willing to believe that you cannot get a null back as a top-level object. I know for a fact that it will ignore inner XML which it does not recognize, including XML which has the correct name but the wrong namespace. Those will not be deserialized, and so, will remain null.
John Saunders
ahhh now it all makes sense, thanks so much :D. This is were I wish there was code contracts in the Xml Serializer with Contract.Ensure(Contract.Result<object> != null)
Miau
@Miau: the XML Serializer was written in .NET 1.0, long before code contracts. It is very unlikely to change in the future, except perhaps to fix critical security bugs.
John Saunders
A: 

Appearantly, you can view or download the code for the System.Xml part of the .NET framework. This lets you look in the source code to determine when it returns null.

Sjoerd