locked
Why do I get this error (This document already has a 'DocumentElement' node) RRS feed

  • Question

  • I am new to using asp.net, and I am chashing and buring at step 1

     

    What I am trying to do is to server up xml data in a web form, which I thought would be pretty straight forward.

     

    Here is the layout of the xml data:

     

     

    <?xml version="1.0" encoding="utf-8" ?>

    <?xml-stylesheet type="text/xsl" href="test1.xsl"?>

    <catalog>

     <childelmt>
      <grandchildelmt1>text</grandchildelmt1>
      <grandchildelmt2>text</grandchildelmt2>
      <grandchildelmt3>text</grandchildelmt3>
      <grandchildelmt4>text</grandchildelmt4>
      <grandchildelmt5>text</grandchildelmt6>
      <grandchildelmt6>text</grandchildelmt6>
      ...
      <grandchildelmtX>text</grandchildelmtX>
     </childelmt>

     <childelmt>
      <grandchildelmt1>text</grandchildelmt1>
      <grandchildelmt2>text</grandchildelmt2>
      <grandchildelmt3>text</grandchildelmt3>
      <grandchildelmt4>text</grandchildelmt4>
      <grandchildelmt5>text</grandchildelmt6>
      <grandchildelmt6>text</grandchildelmt6>
      ...
      <grandchildelmtX>text</grandchildelmtX>
     </childelmt>

     <childelmt>
      <grandchildelmt1>text</grandchildelmt1>
      <grandchildelmt2>text</grandchildelmt2>
      <grandchildelmt3>text</grandchildelmt3>
      <grandchildelmt4>text</grandchildelmt4>
      <grandchildelmt5>text</grandchildelmt6>
      <grandchildelmt6>text</grandchildelmt6>
      ...
      <grandchildelmtX>text</grandchildelmtX>
     </childelmt>

      ...
      ...
      ...

     

     <childelmt>
      <grandchildelmt1>text</grandchildelmt1>
      <grandchildelmt2>text</grandchildelmt2>
      <grandchildelmt3>text</grandchildelmt3>
      <grandchildelmt4>text</grandchildelmt4>
      <grandchildelmt5>text</grandchildelmt6>
      <grandchildelmt6>text</grandchildelmt6>
      ...
      <grandchildelmtX>text</grandchildelmtX>
     </childelmt> 

    </catalog>

    Here is the test1.xsl file that  is referenced

     

    <?xml version="1.0" encoding="utf-8" ?>

    <!-- File Name: test1.xsl -->


    <xslTongue Tiedtylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:fo="
    http://www.w3.org/2005/02/xpath-functions">

        
     <xsl:template match="/">
       
     
     <h2>Page Title</h2>

      
        <xsl:variable name="test2" select="catalog/childelmt[grandchildelmtY='text']"/>
           
      <table border="1" cellpadding="5">
      
       <tr>
        <td>Row Heading</td> 
        <xsl:for-each select="$test2">
        <td><xsl:value-of select="grandchildelmt1"/></td>
        </xsl:for-each>
       </tr>
       
       <tr>
        <td>Row Heading</td> 
        <xsl:for-each select="$test2">
        <td><xsl:value-of select="grandchildelmt2"/></td>
        </xsl:for-each>
       </tr>
       
          
       <tr>
        <td>Row Heading</td> 
        <xsl:for-each select="$test2">
        <td><xsl:value-of select="grandchildelmt3"/></td>
        </xsl:for-each>
       </tr>
       
       <tr>
        <td>Row Heading</td> 
        <xsl:for-each select="$test2">
        <td><xsl:value-of select="grandchildelmt4"/></td>
        </xsl:for-each>
       </tr>
       
       <tr>
        <td>Row Heading</td> 
        <xsl:for-each select="$test2">
        <td><xsl:value-of select="grandchildelmt5"/></td>
        </xsl:for-each>
       </tr>
       
          
       <tr>
        <td>Row Heading</td> 
        <xsl:for-each select="$test2">
        <td><xsl:value-of select="grandchildelmt6"/></td>
        </xsl:for-each>
       </tr>

       .....
       .....
       .....
       .....
       
       <tr>
        <td>Row Heading</td> 
        <xsl:for-each select="$test2">
        <td><xsl:value-of select="grandchildelmtX"/></td>
        </xsl:for-each>
       </tr>
       
       
       
      </table>

     </xsl:template>
    </xslTongue Tiedtylesheet>


     

    What this stylesheet does is get data from each node and lay it out by rows instead of columns.  If I display the xml file in a browser, the page is rendered as I would like.

     

    I followed the walktrhough document "Creating a web page to display XML data, located at -- http://msdn2.microsoft.com/en-us/library/13ftcwy9(VS.80).aspx and I can get it work with the data provided in the example, but I can't get this concept to work with my data.

     

    What I get when I try to render the aspx page is the criptic asp error (see below) which is greek to me.  I have missed something somewhere, I just can't  figure out what it is. 

     

    Help Please! 

     

     

    Server Error in '/test' Application.

    This document already has a 'DocumentElement' node.

    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.InvalidOperationException: This document already has a 'DocumentElement' node.

    Source Error:

    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

    Stack Trace:

    [InvalidOperationException: This document already has a 'DocumentElement' node.]
       System.Xml.XmlDocument.IsValidChildType(XmlNodeType type) +1891757
       System.Xml.XmlDocument.AppendChildForLoad(XmlNode newChild, XmlDocument doc) +25
       System.Xml.XmlLoader.LoadDocSequence(XmlDocument parentDoc) +20
       System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace) +162
       System.Xml.XmlDocument.Load(XmlReader reader) +96
       System.Web.UI.WebControls.XmlDataSource.PopulateXmlDocument(XmlDocument document, CacheDependency& dataCacheDependency, CacheDependency& transformCacheDependency) +305
       System.Web.UI.WebControls.XmlDataSource.GetXmlDocument() +154
       System.Web.UI.WebControls.XmlDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments) +24
       System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +17
       System.Web.UI.WebControls.DataBoundControl.PerformSelect() +149
       System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +70
       System.Web.UI.WebControls.GridView.DataBind() +4
       System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +82
       System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls() +69
       System.Web.UI.Control.EnsureChildControls() +87
       System.Web.UI.Control.PreRenderRecursiveInternal() +41
       System.Web.UI.Control.PreRenderRecursiveInternal() +161
       System.Web.UI.Control.PreRenderRecursiveInternal() +161
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1360
    


    Version Information: Microsoft .NET Framework Version:2.0.50727.312; ASP.NET Version:2.0.50727.312

     

     

    Saturday, May 19, 2007 5:42 PM

Answers

  • Please check if you have used method AppendChild() under document. If this is the case you will need to do it for "document.DocumentElement" instead of just docuemnt.

    Attempting to append another child element directly to the document will cause the above exception.
    Monday, September 21, 2009 12:36 PM

All replies

  • The error message sounds as if your XML markup is not well-formed. As you have only posted snippets it is hard to tell where the error is.

    In that snippet you for instance have

    Code Snippet

    <grandchildelmt5>text</grandchildelmt6>

     which is certainly not well-formed.

    Sunday, May 20, 2007 12:02 PM
  • The real data is well formed, what I put up was just a quick generic representation of the real data, and xslt styleshhet.  I have checked the real data and styleheet for well-formness using Altova XMLspy; the the data and the stylesheet are well formed.  Just to make sure that the well-formness check is accurate, I created a mismatch start and end tag in the data, and Altova XMLsky alerted me to the error.
    Sunday, May 20, 2007 3:05 PM
  • Please check if you have used method AppendChild() under document. If this is the case you will need to do it for "document.DocumentElement" instead of just docuemnt.

    Attempting to append another child element directly to the document will cause the above exception.
    Monday, September 21, 2009 12:36 PM
  • Did you find the fix for this? I'm experienceing the same problem.
    For my case, the problem does not always occur so... I encounter this problem when I'm processing a large data.

    Rafferty
    Wednesday, September 30, 2009 4:23 AM