I had this case again today, where I got “Root element is missing”. The code was working, I made some changes, and boom, an existing code path started blowing up. It was actually a trace routine, where I had the following code:
<pre>
xmlDoc = msgWhatever;
//then I passed as a parameter:
CallCSharpMethod(xmlDoc.OuterXml)...
</pre>
So here is the code I added, above the section that bombed:
<pre>
// Avoid compile erorr "Use of unconstructed message 'msgCanonicalErr'
// in the common error handler after the end of the big Decide Block
xmlDoc.LoadXml("<Dummy />");
msgCanonicalErr = xmlDoc;
</pre>
The simple fix was to create another xmlDoc variable with another name:
<pre>
// Avoid compile erorr "Use of unconstructed message 'msgCanonicalErr'
// in the common error handler after the end of the big Decide Block
xmlDocInitializer.LoadXml("<Dummy />");
msgCanonicalErr = xmlDocInitializer;
</pre>
I think another solution would have been to reset the xmlDocument variable, but I preferred the above approach:
<pre>
xmlDoc = new System.XmlDocument();
</pre>