Is there a way write into the same OpenXmlPart
that is being read by an OpenXmlPartReader
? I'm going to detect things with the reader and potentially write things in place with an OpenXmlWriter
. Here is a sample that does not work:
var ms = new MemoryStream();
var doc = WordprocessingDocument.Create(ms, WordprocessingDocumentType.Document);
MainDocumentPart part = doc.AddMainDocumentPart();
part.Document = new Document(new Body(new Paragraph(), new Paragraph()));
doc.Save();
var stream = part.GetStream();
var reader = OpenXmlReader.Create(stream);
var writer = OpenXmlWriter.Create(stream);
reader.Read(); // read w:document
reader.ReadFirstChild(); // read w:body
reader.ReadFirstChild(); // read 1st w:p
writer.WriteElement(new Table()); // try to insert w:tbl
doc.Save(); // observe only the 2 paragraphs in the `w:body`.
It looks like under the covers the Open XML SDK is using XmlReader
and XmlWriter
under the covers, so perhaps there is a way to synchronize those?
So it appears that there is no way to synchronize the
OpenXmlReader
andOpenXmlWriter
out of the box.What I ended up doing was cloning the original document, clearing out the parts and writing into them conditionally, while reading out of the
OpenXmlReader
.Here is a simplified model of what I did: