Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What types of Exceptions can the XmlSerializer.Deserialize method throw?

For this method, XmlSerializer.Deserialize, what kinds of exception may be thrown? XmlException? InvalidOperationException? I did not find any exception description information from this method. My question is what kinds of exception could be thrown from this method?

http://msdn.microsoft.com/en-us/library/dsh84875.aspx

I am using VSTS2008 + C# + .Net.

thanks in advance, George

like image 309
George2 Avatar asked Aug 25 '09 04:08

George2


3 Answers

Looks like primarily InvalidOperationException.

If you go through the documentation for each of the overloads, it will give you more details. For example, see XmlSerializer.Deserialize Method (XmlReader)

The InvalidOperationException will contain more details about the specific error in its InnerException property.

Edit:

The XmlSerializer.Deserialize Method (XmlSerializationReader) can throw a NotImplementedException, but it is an internal API and is not meant to be used by your code, so don't worry about it.

Edit 2:

This code:

var ms = new System.IO.MemoryStream();
var deser = new System.Xml.Serialization.XmlSerializer(typeof(string));
deser.Deserialize(ms);

throws:

System.InvalidOperationException: There is an error in XML document (0, 0). ---
System.Xml.XmlException: Root element is missing.
  at System.Xml.XmlTextReaderImpl.Throw(Exception e)
... <snip> ...

So it really looks like the framework will always throw an InvalidOperationException.

Really, unless you're worried about mistakenly catching exceptions like ThreadAbortException, you are probably safest catching all exceptions...

Edit 3:

Using Reflector: The Deserialize(stream) method reads the stream using an XmlTextReader and calls the XmlSerializer.Deserialize Method (XmlReader, String). That method throws an InvalidOperationException on error (according to the docs).

Edit 4:

Deserialize(stream) can also throw a NullReferenceException if stream is null, because it calls the XmlTextReader(Stream) constructor.

like image 50
Nader Shirazie Avatar answered Nov 07 '22 13:11

Nader Shirazie


Exceptions from the XmlSerializer

Diagnosing the source of these problems can be tricky at first, because the exceptions from the XmlSerializer do not seem to provide a lot of information about the cause of their occurance; at least, they do not provide the information in a spot where developers typically would look.

In most cases, Serialize, Deserialize and even the XmlSerializer constructor throw a rather generic System.InvalidOperationException when an error occurs. This exception type can occur in many places in the .NET Framework; it is not specific to the XmlSerializer at all. To make matters worse, the exception's Message property only yields very generic information, as well.

This and other very handy tips about exceptions in the XmlSerializer can be found on the document Troubleshooting Common Problems with the XmlSerializer.

like image 25
Flávio Ivan Avatar answered Nov 07 '22 15:11

Flávio Ivan


George, because there is no exception contract in .NET, the best practice is to catch any specific exceptions that you may want to do special processing for, but to also have a catch-all exception handler that handles all failures appropriately.

I have implemented several XML serialization solutions using the built-in .NET serialization, and have in all cases just used a catch-all except block, which walks the innerexceptions of the caught exception, adding all error messages and exception class types to a string message. Doing it like that has always provided enough information to debug any serialization issues.

On a related note, what I normally do is to add a debug log level which logs the full xml to my application's log, so that I can inspect it to try and figure out what when wrong when debugging a serilization issue.

like image 24
Joon Avatar answered Nov 07 '22 13:11

Joon