私のコードは、docx ドキュメントを作成し、複数のドキュメントからパーツを取得して 1 つにマージすることを目的としています。
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();
}
コードは抜粋のみで、docx
Office 2013 で作成した場合は正常に動作しますが、ドキュメントがdoc
Office 2013 で docx として保存された (古いバージョンの Office で作成された)場合は失敗しますtargetDoc.MainDocumentPart.Document.Save()
。実行すると、次のエラーが発生します。
{"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\"."}
どういう意味ですか?どうすれば解決できますか?