0

I am using XDocument and XElement to read the XML. While running the website from visual studio 2012 there is no problem in reading. After publishing i am getting "Root element is missing" exception.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Xml.XmlException: Root element is missing.

Please find the XML below

<CTReport>
  <ReportHeader>
    <UserID>1508ac07-0070-46b9-84f4-533b83cb3770</UserID>
  </ReportHeader>
  <ReportInfoToDisplay>
    <Report PlaceHolder="7">
      <Info Name="Average Mix report" TranslatedText="" ShowPrice="1" />
      <ReportControls>
        <ControlInfo Type="Date" Name="Date" TranslatedText="" Data="!" />
        <ControlInfo Type="Lookup" Name="Scale operator" TranslatedText="" Data="" />
        <ControlInfo Type="Lookup" Name="Display Jobs of" TranslatedText="" Data="Normal!Layaway!" />
        <ControlInfo Type="Combo" Name="SortBy" TranslatedText="SortBy" Data="Sort by mixer + product group!Sort by product group" />
      </ReportControls>
    </Report>
    <Report />
    <Report PlaceHolder="8">
      <Info Name="Mix report" TranslatedText="" ShowPrice="0" />
      <ReportControls>
        <ControlInfo Type="Date" Name="Date" TranslatedText="" Data="!" />
        <ControlInfo Type="Lookup" Name="Scale operator" TranslatedText="" Data="" />
        <ControlInfo Type="Combo" Name="SortBy" TranslatedText="SortBy" Data="Sort by mixer + product group!Sort by product group" />
      </ReportControls>
    </Report>
    <Report PlaceHolder="9">
      <Info Name="Scale report" TranslatedText="" ShowPrice="0" />
      <ReportControls>
        <ControlInfo Type="Date" Name="Date" TranslatedText="" Data="!" />
        <ControlInfo Type="String" Name="Job reference" TranslatedText="" Data="" />
        <ControlInfo Type="String" Name="Job ID" TranslatedText="" Data="" />
        <ControlInfo Type="Lookup" Name="Scale operator" TranslatedText="" Data="" />
      </ReportControls>
    </Report>
  </ReportInfoToDisplay>
</CTReport>

Code:

  XDocument xml = XDocument.Parse(reportXML);

reportXML is string.

Please note while XDocument is created from XML file there is no exception

4

1 回答 1

1

The problem is almost certainly that the XML you're trying to parse does not match that you've posted above.

There are a few scenarios:

  1. There is more than one CTReport element (ie you've got two sets of reports in the XML)
  2. There is no CTReport at all (so at the top level you'd have ReportHeader and ReportInfoToDisplay
  3. There is a single CTReport element as expected, but then there is some other element also at the root level.
  4. Your document is entirely empty.

The best thing you could do would be to add a line to your code to output the contents of reportXML to a text file so that you can have a better look at it and make sure its what you're expecting. You can do this by putting a line similar to the one below just before you try to parse the XML:

System.IO.File.WriteAllText(@"<Put an output file path here>", reportXML);
于 2012-09-13T08:21:45.060 回答