Open-XML-SDK: ToFlatOpcString fails with generated document
Description
ToFlatOpcString
throws “Root element is missing.” I’m trying to put this format into a VSTO add-in. When I enumerate wordprocessingDocument.Package.GetParts()
, I get the relationship part back as an empty string (ContentType = "application/vnd.openxmlformats-package.relationships+xml"
). Shouldn’t AddMainDocumentPart
take care of this under the covers?
Information
- .NET Target: .NET Core 2.0, .NET Framework 4.7
- DocumentFormat.OpenXml Version: 2.8.1
Repro
using System;
using System.IO;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
using (MemoryStream stream = new MemoryStream())
using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Create(stream, WordprocessingDocumentType.Document))
{
MainDocumentPart mainDocumentPart = wordprocessingDocument.AddMainDocumentPart();
Body body = new Body(new Paragraph(new Run(new RunProperties(new Bold()),
new Text("Hello world this should be bold"))));
mainDocumentPart.Document = new Document(body);
wordprocessingDocument.Save();
Console.WriteLine(wordprocessingDocument.ToFlatOpcDocument());
}
}
}
}
Observed
Exception:
System.Xml.XmlException: Root element is missing.
at System.Xml.XmlTextReaderImpl.Throw(Exception e)
at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
at System.Xml.XmlTextReaderImpl.Read()
at System.Xml.XmlReader.MoveToContent()
at System.Xml.Linq.XElement.Load(XmlReader reader, LoadOptions options)
at DocumentFormat.OpenXml.Packaging.OpenXmlPackage.GetContentsAsXml(PackagePart part)
at System.Linq.Enumerable.SelectEnumerableIterator`2.MoveNext()
at System.Xml.Linq.XContainer.AddContentSkipNotify(Object content)
at System.Xml.Linq.XContainer.AddContentSkipNotify(Object content)
at DocumentFormat.OpenXml.Packaging.OpenXmlPackage.ToFlatOpcDocument(XProcessingInstruction instruction)
at ConsoleApp1.Program.Main(String[] args)
Expected
That ToFlatOpcString
doesn’t throw and I get the resulting XML string.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 20 (9 by maintainers)
Commits related to this issue
- adding tests for dynamically created documents that fails currently due to #380 — committed to daniel-white/Open-XML-SDK by deleted user 6 years ago
Fair enough.
On another note, the unit tests don’t test this path at all, perhaps for this very reason.
What’s troubling is that this is failing in .NET framework in the same way the .NET Core build does. adding a call to
doc.Package.Flush
fixes it on .NET framework. Are you guys going for parity here? Would a#if NETFRAMEWORK
conditional inside ofSave
make sense to at least get the .net framework working?@twsouthwick not a regression with .net 4.7 or .net core 2.0 using v2.7.2. same exception.