Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am i getting a OpenXmlUnknownElement?

Tags:

arrays

c#

openxml

This is not really a question since I already have found the reason for this problemRetrieving descendants from OpenXml body

The descendants are retrieved using this code.

using System.IO;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
namespace Testolini
{
    class Program
    {
        static void Main(string[] args)
        {
            var filename = Path.GetTempFileName();
            var word = WordprocessingDocument.Create(filename, DocumentFormat.OpenXml.WordprocessingDocumentType.Document);
            word.AddMainDocumentPart();
            word.MainDocumentPart.Document = new Document(
                                                new Body(
                                                    new Paragraph(
                                                        new Run(
                                                            new Paragraph(
                                                                new Run(
                                                                    new Text("test1"))),
                                                            new Paragraph(
                                                                new Run(
                                                                    new Text("test2"))),
                                                            new Paragraph(
                                                                new Run(
                                                                    new Text("test3")))))));
            word.Close();

            using (var memorystream = new MemoryStream(File.ReadAllBytes(filename)))
            {
                var word2 = WordprocessingDocument.Open(memorystream, true);

                var descendants = word2.MainDocumentPart.Document.Body.Descendants();

                word.Close();

            }
        }
    }
}

If you get the same problem. It can be because the XML fil does not validate with the ECMA standard. In my case the problem was that i had nested paragraphs.

The problem shows up when I opened the document using bytearray and memorystream. It looks like the elements is validated and if the validation fails it becames an OpenXmlUnknownElement.

If anyone have better explanation and maybe a more precise reason for this problem I would love to learn more about it.

like image 218
Stian Standahl Avatar asked Feb 19 '23 07:02

Stian Standahl


2 Answers

A Run cannot contain another Paragraph.

Here is a list of valid child elements for a Run:

annotationRef (Comment Information Block)
br (Break)
commentReference (Comment Content Reference Mark)
contentPart (Content Part)
continuationSeparator (Continuation Separator Mark)
cr (Carriage Return)
dayLong (Date Block - Long Day Format)
dayShort (Date Block - Short Day Format)
delInstrText (Deleted Field Code)
delText (Deleted Text)
drawing (DrawingML Object)
endnoteRef (Endnote Reference Mark)
endnoteReference (Endnote Reference)
fldChar (Complex Field Character)
footnoteRef (Footnote Reference Mark)
footnoteReference (Footnote Reference)
instrText (Field Code)
lastRenderedPageBreak (Position of Last Calculated Page Break)
monthLong (Date Block - Long Month Format)
monthShort (Date Block - Short Month Format)
noBreakHyphen (Non Breaking Hyphen Character)
object (Embedded Object)
pgNum (Page Number Block)
ptab (Absolute Position Tab Character)
rPr (Run Properties)
ruby (Phonetic Guide)
separator (Footnote/Endnote Separator Mark)
softHyphen (Optional Hyphen Character)
sym (Symbol Character)
t (Text)
tab (Tab Character)
yearLong (Date Block - Long Year Format)
yearShort (Date Block - Short Year Format)

Taken from MSDN.

Why do you need "nested" paragraphs?

like image 152
jn1kk Avatar answered Feb 21 '23 02:02

jn1kk


It seems the document is in an invalid state. I think this happens when you alter the document so that it contains children beneath parents that should not be related that way.

This might be of help: http://msdn.microsoft.com/en-us/library/office/bb497334.aspx

like image 21
Maarten Avatar answered Feb 21 '23 01:02

Maarten