“Root element is missing” error when trying to create an XPathDocument object from Request.InputStream

by S.Y.M. Wong-A-Ton

Lean how to avoid getting the error message 'Root element is missing' when submitting an InfoPath form data to a web server.

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.

 Subscribe (RSS | Email)

Related Posts

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

Copyright: This article may not be used on web sites (whether personal or otherwise), copied, disseminated, altered, printed, published, broadcasted, or reproduced in any way without an expressed written consent of S.Y.M. Wong-A-Ton. Usage of techniques demonstrated in this article may be used within any Microsoft InfoPath project. This article is provided without any warranties. Copyright for this article is non-transferrable and remains with the author, S.Y.M. Wong-A-Ton.

Working with InfoPath