none
OPENXML generated Excel Document - A document must contain exactly one root element. Line 1, column 0 RRS feed

  • Question

  • I am trying to create and write to excel. For the template, I simply copied over code from OPENXML Productivity tool for an existing excel file, and then modified the code to add my rows of data when a new file is created from that code. However, I seem to be having some issue getting this to work correctly. I keep getting a

    Replaced Part: /xl/worksheets/sheet2.xml part with XML error. A document must contain exactly one root element. Line 1, column 0.

    error which I am still unable to correctly debug. Also, in the original template, there were 3 sheets in my workbook. I made sure to delete two of them so I can have just the code for the one created, as I assumed the OpenXML 2.0 Productivity tool would only generate code for the sheet that existed at the time document code was put together. But what happens is that the generated file has all 4 of the original sheets in place, but seems to not be able to put together the headers on each.

    Wanted to get some hints as to where to go to find this problem and fix it. thanks in advance.

       public void CreatePackage(string filePath)
           
    {
                using
    (SpreadsheetDocument package = SpreadsheetDocument.Create(filePath, SpreadsheetDocumentType.Workbook))
               
    {
                   
    CreateParts(package);
               
    }
           
    }

           
    private void CreateParts(SpreadsheetDocument document)
           
    {
               
    ExtendedFilePropertiesPart extendedFilePropertiesPart1 = document.AddNewPart<ExtendedFilePropertiesPart>("rId3");
               
    GenerateExtendedFilePropertiesPart1Content(extendedFilePropertiesPart1);

               
    WorkbookPart workbookPart1 = document.AddWorkbookPart();
               
    GenerateWorkbookPart1Content(workbookPart1);

               
    WorkbookStylesPart workbookStylesPart1 = workbookPart1.AddNewPart<WorkbookStylesPart>("rId3");
               
    GenerateWorkbookStylesPart1Content(workbookStylesPart1);

               
    ThemePart themePart1 = workbookPart1.AddNewPart<ThemePart>("rId2");
               
    GenerateThemePart1Content(themePart1);

               
    WorksheetPart worksheetPart1 = workbookPart1.AddNewPart<WorksheetPart>("rId1");
               
    GenerateWorksheetPart1Content(worksheetPart1);

               
    WorksheetCommentsPart worksheetCommentsPart1 = worksheetPart1.AddNewPart<WorksheetCommentsPart>("rId2");
               
    GenerateWorksheetCommentsPart1Content(worksheetCommentsPart1);

               
    VmlDrawingPart vmlDrawingPart1 = worksheetPart1.AddNewPart<VmlDrawingPart>("rId1");
               
    GenerateVmlDrawingPart1Content(vmlDrawingPart1);

               
    SharedStringTablePart sharedStringTablePart1 = workbookPart1.AddNewPart<SharedStringTablePart>("rId4");
               
    GenerateSharedStringTablePart1Content(sharedStringTablePart1);

    tokorie@gmail.com

    • Moved by Leo_GaoModerator Tuesday, October 2, 2012 5:38 AM (From:Excel for Developers)
    Monday, October 1, 2012 9:49 PM

Answers

  • Hi DataByter,

    Thanks for posting in the MSDN Forum.

    It's based on my experience that the problem is in the way you are structuring the XML; as the error explains, the XML document must have exactly one root element. There are some examples

    A valid XML document

    <users>
     <user>
      <name>Mike</name>
      <age>17</age>
     </user>
     <user>
      <name>Lina</name>
      <age>18</age>
     </user>
    </users>

    An invalid XML document

    <user>
      <name>Mike</name>
      <age>17</age>
     </user>
     <user>
      <name>Lina</name>
      <age>18</age>
    </user>

    In the first example, every element in the document is a child of users, while in the second one there are two users floating around. So could you have a check for your XML document or share it in the forum for doing further research?

    Hope this helps.

    Best Regards, 


    Leo_Gao [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, October 3, 2012 3:26 AM
    Moderator

All replies

  • Hi DataByter,

    Thanks for posting in the MSDN Forum.

    It's based on my experience that the problem is in the way you are structuring the XML; as the error explains, the XML document must have exactly one root element. There are some examples

    A valid XML document

    <users>
     <user>
      <name>Mike</name>
      <age>17</age>
     </user>
     <user>
      <name>Lina</name>
      <age>18</age>
     </user>
    </users>

    An invalid XML document

    <user>
      <name>Mike</name>
      <age>17</age>
     </user>
     <user>
      <name>Lina</name>
      <age>18</age>
    </user>

    In the first example, every element in the document is a child of users, while in the second one there are two users floating around. So could you have a check for your XML document or share it in the forum for doing further research?

    Hope this helps.

    Best Regards, 


    Leo_Gao [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, October 3, 2012 3:26 AM
    Moderator
  • This is not the answer at all!!!  The question was about how to use the OpenXML API to create a valid Excel spreadsheet, and in this case the raw XML is completely hidden from the developer.  Please mark this question as unanswered, especially since the documentation examples for OpenXML actually produce this error as well.  If I am able to figure out why I will post a solution.
    Wednesday, January 8, 2014 9:20 PM
  • Hi Jamie

    I don't think the original moderator is still active. The discussion was marked as answered because the OP never came back and provided additional information as requested.

    My recommendation would be for you to start a new discussion, with a link to the example which is producing the problem and, if you're changing anything at all, the exact steps to reproduce the problem.

    FWIW in my experience the error can occur if an XML part is "put together" incorrectly (new nodes appended to the wrong place in the hierarchy). While this is abstracted for the developer, the result will still be the same if things aren't put together as required the by the schema... 


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, January 9, 2014 3:21 PM
    Moderator