My code is intended to create a docx document, taking the parts from more documents and merge them into one.
private void CopyContent(WordprocessingDocument sourceDoc, WordprocessingDocument targetDoc)
{
// copy parts from source document to new document
foreach (var part in sourceDoc.Parts)
targetDoc.AddPart(part.OpenXmlPart, part.RelationshipId);
using (var sr = new StreamReader(sourceDoc.MainDocumentPart.GetStream()))
using (var sw = new StreamWriter(targetDoc.MainDocumentPart.GetStream(FileMode.Create)))
{
sw.Write(sr.ReadToEnd());
}
targetDoc.MainDocumentPart.Document.Save();
}
The code is only a snipped, it works fine for docx
created by Office 2013, but it fails if the document was a doc
(created by an older version of office) saved as docx by Office 2013.
It rises the following error once targetDoc.MainDocumentPart.Document.Save()
is executed.
{"The root XML element \"http://purl.oclc.org/ooxml/wordprocessingml/main:document\" in the part is incorrect. The expected root XML element is: \"http://schemas.openxmlformats.org/wordprocessingml/2006/main:document\"."}
What does it mean? How could I solve it?