Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug with Open XML SDK #1777

Closed
dbeaudoinl99 opened this issue Aug 25, 2024 · 2 comments
Closed

Bug with Open XML SDK #1777

dbeaudoinl99 opened this issue Aug 25, 2024 · 2 comments
Assignees

Comments

@dbeaudoinl99
Copy link

dbeaudoinl99 commented Aug 25, 2024

Describe the bug

  1. Updated a customxml part (item1.xml), then saved the word document.
  2. After saving the word file, I reviewed the customxml part (item1.xml) and noted that it did update correctly.
  3. Opened the word doc and noted that the data in the xml gets pulled into the word document as expected.
  4. Without saving the word doc, I unpacked the doc file and reviewed document.xml file and noted that it did NOT update with the updated customxml part.
  5. If I perform a Save As and examine the file again, the document.xml file is updated.

Screenshots
If applicable, add screenshots to help explain your problem.

To Reproduce
// Please add a self-contained, minimum viable repro of the issue.
// If you require external resources, please provide a gist or GitHub repro
// An Xunit style test is preferred, but a console application would work too.

Using the latest version. 3.1.0 and .Net 8

Using 
 var os = new OpenSettings
 {
     MarkupCompatibilityProcessSettings = new MarkupCompatibilityProcessSettings(
 MarkupCompatibilityProcessMode.ProcessAllParts, FileFormatVersions.Office2007)
 };

Steps to reproduce the behavior:

beginning of code:

 //Word Doc - Decode the word doc from SharePoint. 
 byte[] byteWordDoc = Convert.FromBase64String(strWordDocBase64);
 MemoryStream streamWordDoc = new MemoryStream();
 streamWordDoc.Write(byteWordDoc, 0, byteWordDoc.Length);

 //XML File
 byte[] byteXMlFile = Convert.FromBase64String(strXMLFileBase64);
 XElement xmlfile = XElement.Parse(System.Text.Encoding.UTF8.GetString(byteXMlFile));


 streamWordDoc.Seek(0, SeekOrigin.Begin);

 //Get the custom part for index1.xml
 foreach (CustomXmlPart part in wordDoc.MainDocumentPart.CustomXmlParts)
 {
     
     if (part.Uri.OriginalString.IndexOf(strWordDocNamespace) != -1)
     {
         mainXMLPart = part;
         break;
     }
 }

 using (Stream stream = mainXMLPart.GetStream(FileMode.Create, FileAccess.Write))
 {
     using (XmlWriter partXMLWriter = XmlWriter.Create(stream))
     {
         xmlfile.Save(partXMLWriter);
         

     };

 }

 streamWordDoc.Seek(0, SeekOrigin.Begin);
 wordDoc.Save();
 wordDoc.Dispose();

//Convert to base64 and return as JSON payload.
var wordDocBase64 = new { worddoc = Convert.ToBase64String(streamWordDoc2.ToArray()) };

Observed behavior
See notes above.

Expected behavior
The document.xml to be updated.

Desktop (please complete the following information):

  • OS: Windows 11
  • Office version 16
  • .NET Target: 8
  • DocumentFormat.OpenXml Version: 3.1.0

Additional context
Add any other context about the problem here.

@ThomasBarnekow
Copy link
Collaborator

@dbeaudoinl99, as it has been a while since you posted this issue, have you solved it in the meantime?

Here are some things I noted:

Firstly, you did not provide a "complete" repro. For example, I can't see how you open the WordprocessingDocument. I can only guess you open it on the streamWordDoc.

Secondly, the following lines of code do not look right:

 streamWordDoc.Seek(0, SeekOrigin.Begin);
 wordDoc.Save();
 wordDoc.Dispose();

If you've opened the wordDoc on the streamWordDoc stream, it does not make sense to reset its position before saving the document, which you also do not have to do if you've created the WordprocessingDocument instance correctly. Here is how this would look like if you need to work with the stream after having closed the WordprocessingDocument:

using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(streamWordDoc, true))
{
    // Do what you need to do with the wordDoc and its parts.
    // However, altering the stream at this point might not be a good idea.
}

// At this point, the `wordDoc` will have been auto-saved and properly disposed (and closed),
// so no need to call Save() and Dispose().
// You can now continue working with the stream.
streamWordDoc.Seek(0, SeekOrigin.Begin);

@twsouthwick
Copy link
Member

Thanks @ThomasBarnekow for taking a look here.

@dbeaudoinl99 this is most likely the culprit I'm going to close it for now - please reopen if you have more information

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants