Error messages tend, by default, not to be overly helpful. Sometimes though, they're just plain baffling. This is the case for the Error in XML Document error. I received this error when trying to add my current project to a fresh portal. Every time I went to Add Web Parts and dragged/dropped the part into a zone, the Error in XML Document (1,40) message would appear in the side menu in red letters.
Well, after much digging, I have figured out why it does this. It is caused by an unhandled exception in a constructor. I had missed a few spots in my code when I went back and refactored my error handling, and so the web part didn’t gracefully handle empty tables. Fixed the problem (and added a general try/catch to my constructor) and I was able to add the web part back in.
To test: Create a new web part, verify it is safe and working, then add the following:
|
public YourClassName(){ throw new Exception(); }
Public Sub New() Throw New Exception() End Sub |
 C# |
 VB |
|
Rebuild/deploy. The web part that was already added in will display a ‘Cannot desieralize’ error message. If you try to add it in again, you will get the XML Document error. To fix change the constructor to this:
|
public YourClassName(){ try { throw new Exception(); } catch { } }
Public Sub New() Try Throw New Exception() Catch err As Exception End Try End Sub
|
 C# |
 VB |
|
Rebuid/deploy. Error will be fixed.