ToFlatOpcString fails with generated document
See original GitHub issueDescription
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.
Issue Analytics
- State:
- Created 6 years ago
- Comments:20 (9 by maintainers)
Top Results From Across the Web
ToFlatOpcString fails with generated document · Issue #380
ToFlatOpcString throws "Root element is missing." I'm trying to put this format into a VSTO add-in. When I enumerate wordprocessingDocument.
Read more >Range.InsertXML throws when inserting Open XML
This article explains in simple terms what the minimum Word Open XML is for some kinds of content and how to use the...
Read more >DocumentFormat.OpenXml 2.20.0
High-performance generation of word-processing documents, spreadsheets, and presentations. - Populating content in Word files from an XML data source.
Read more >Create a word processing document by providing a file name
In the Open XML SDK, the WordprocessingDocument class represents a Word document package. To create a Word document, you create an instance ...
Read more >Validating of OpenXml generated documents or The file ...
My current task involves generating a MS Word document from C#. ... The error message sometimes gives you a column inside of the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.