Several people have submitted feedback to me in a timespan of about 2 months to alert me that my code in the ASP Alliance article “Saving InfoPath Forms to SQL Server 2005 as XML” is causing the following error: “Root element is missing”. So I thought: If 3 people report the same error, there must be something wrong with the code, right? As it turns out, the code is a tad incomplete; my apologies for that.
The Default.aspx page is meant to be called by an InfoPath form, not a normal user. If it is called by a user as is the case when you run the web project or access the page via a browser, the Request.InputStream will not contain any data, which in turn will result in the error “Root element is missing” when you try to create an XPathDocument object from the Request.InputStream.
The best way to solve this problem – apart from adding error handling to the page – is by enclosing the code of Listing 4 in the article by the following code:
if (Request.InputStream != null && Request.InputStream.Length > 0)
{
...
}
This way the code will only run if Request.InputStream contains data, which is the case if/when you’ve set up an InfoPath form template to submit to the ASP.NET web page.
I hope this clears up the confusion that the code in the article has caused and that this will solve the error if you have been having issues with the code.
Thank you for reading the articles that I write and publish, and please take a few seconds to rate them, because that’s the only way I will know how useful they are to you.

Comments to this post were closed 30 days after it was published.