Open-XML-SDK
Open-XML-SDK copied to clipboard
Bug with Open XML SDK
Describe the bug
- Updated a customxml part (item1.xml), then saved the word document.
- After saving the word file, I reviewed the customxml part (item1.xml) and noted that it did update correctly.
- Opened the word doc and noted that the data in the xml gets pulled into the word document as expected.
- 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.
- 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.