Visual Studio .NET - The Root Element is Missing problem

Asked By mark Pauly on 12-Oct-06 11:12 AM

I have an application which processes a stream received from a request, as in Request.InputStream. I am passing XML data to this for processing. The problem I am having is when I attempt to load the stream produced by Request.InputStream, I get an exception thrown as "The Root Element is missing". I decided to run a debug and write out the stream to an XML file and it looked ok to me, can anybody help me out on this one?

Here is the code I am using:

XmlDocument xDoc = null;
xDoc =
new XmlDocument();
xDoc.Load(request.InputStream);
xDoc.Save(@"C:\Tester.xml");
stream = request.InputStream;

//Process the stream

public static string Process(Stream waveRunnerMessage)
{
   XmlDocument requestDoc =
new XmlDocument();
   XmlDocument responseDoc =
new XmlDocument();

   // attempt to load the request as an xml document
  requestDoc.Load(waveRunnerMessage);
//######### ERROR HERE #######
}

Here is the contents of Tester.xml

<travelmessage versionid="1.4">
  <control language="NL" test="nee">
    <sendersessionid>O042678044</sendersessionid>
    <receiversessionid />
    <date>2006-10-03</date>
    <time>08:37</time>
    <messagesequence />
    <senderid>949</senderid>
    <receiverid />
    <employeeid />
    <requestid>AvailabilityRequest</requestid>
    <responseid />
  </control>
  <trequest>
    <availabilityrequest>
      <packagedetails>
        <packageid>930-3G</packageid>
        <numberofadults>2</numberofadults>
        <numberofchildren>0</numberofchildren>
        <numberofbabies>0</numberofbabies>
        <departuredate>2006-10-07</departuredate>
      </packagedetails>
    </availabilityrequest>
  </trequest>
</travelmessage>

Add Missing root element - F Cali replied to mark Pauly on 12-Oct-06 11:55 AM

As the error message pointed out, your XML is missing the root element.  Try adding the root element as shown below and see if the error goes away:

<root><travelmessage versionid="1.4">
  <control language="NL" test="nee">
    <sendersessionid>O042678044</sendersessionid>
    <receiversessionid />
    <date>2006-10-03</date>
    <time>08:37</time>
    <messagesequence />
    <senderid>949</senderid>
    <receiverid />
    <employeeid />
    <requestid>AvailabilityRequest</requestid>
    <responseid />
  </control>
  <trequest>
    <availabilityrequest>
      <packagedetails>
        <packageid>930-3G</packageid>
        <numberofadults>2</numberofadults>
        <numberofchildren>0</numberofchildren>
        <numberofbabies>0</numberofbabies>
        <departuredate>2006-10-07</departuredate>
      </packagedetails>
    </availabilityrequest>
  </trequest>
</travelmessage></root>

The Root Element is Missing problem - mark Pauly replied to F Cali on 12-Oct-06 12:03 PM

It Still gives me the same message. The xml is well formed and it has a root <travelmessage> element. I tried that suggstion but still get that problem

Response - F Cali replied to mark Pauly on 12-Oct-06 12:16 PM

Try adding the following at the beginning of your XML file and see if the error goes away.

<?xml version="1.0" ?>

Root element is missing problem - mark Pauly replied to F Cali on 12-Oct-06 12:20 PM
Tried that, still gives me the same error, this is a nightmare! Thanks for the help Ronald.
Response - F Cali replied to mark Pauly on 12-Oct-06 12:25 PM

According to the following link:

http://www.thescripts.com/forum/thread176050.html

You have to rewind the stream.

public static string Process(Stream waveRunnerMessage)
{
   XmlDocument requestDoc = new XmlDocument();
   XmlDocument responseDoc = new XmlDocument();

   // attempt to load the request as an xml document

  wareRunnerMessage.Position = 0;
  requestDoc.Load(waveRunnerMessage);
//######### ERROR HERE #######
}

Absolutely fantastic, brilliant, yes!!!!!!!!! - mark Pauly replied to F Cali on 12-Oct-06 12:30 PM
Oh by the way, thank you so much Ronald for that, I have been at this for days.
Shuchita Bora replied to mark Pauly on 15-Jan-10 10:10 AM

There is no problem with the input file.  The stream position set to the end of the stream.  There's no root element because the XmlDocument.Load function thinks the stream has a 0 byte length.  The fix is to simply set the position to 0:

 

StreamXml.Position = 0;
imran replied to Shuchita Bora on 24-Aug-10 03:11 PM
bit 0 will solve the problem