question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ToFlatOpcString fails with generated document

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:20 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
daniel-whitecommented, Jan 17, 2018

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 of Save make sense to at least get the .net framework working?

1reaction
daniel-whitecommented, Jan 17, 2018

@twsouthwick not a regression with .net 4.7 or .net core 2.0 using v2.7.2. same exception.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found