This is not really a question since I already have found the reason for this problem
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.
A
Runcannot contain anotherParagraph.Here is a list of valid child elements for a
Run:Taken from MSDN.
Why do you need “nested” paragraphs?