wordpress hit counter
Re: Converting docx code from Stream to MemoryStream - WordprocessingML - Formats - OpenXML Developer

Re: Converting docx code from Stream to MemoryStream

Formats

Discussions about working with different Open XML Formats

Converting docx code from Stream to MemoryStream

  • rated by 0 users
  • This post has 2 Replies |
  • 1 Follower
  • I wrote some working code which takes a docx document and fills in custom XML.  It currently uses the file system, creating a temporary document, but the source of the input docx document and the sink are both SQL Server, so I'd like to use MemoryStream rather than Stream-based code.  I'm trying to convert it.  My currently flawed Memory-Stream based code goes something like this:

          using (var templateStream = new MemoryStream())
          {
            var templateByteArray = a9DocTemplate.TemplateContent;
            templateStream.Write(templateByteArray, 0, (int)templateByteArray.Length);

            Package templatePackage = Package.Open(templateStream, FileMode.Open, FileAccess.ReadWrite);
            var uriForCustomXml = new Uri("/customXML/item1.xml", UriKind.Relative);
            PackagePart part = templatePackage.GetPart(uriForCustomXml);
            var partReader = new StreamReader(part.GetStream(FileMode.Open, FileAccess.Read));

            // read the data bag into memory
            string xmlToChange = partReader.ReadToEnd();

            // load it into a searchable/updateable DOM (Document Object Model) structure

            var customXml = new XmlDocument();
            customXml.LoadXml(xmlToChange);
            //manipulate the customXml structure here ...

            var documentContent = new DocumentContent(); //object that contains target byte array
            const string partContentType =
              "application/vnd.openxmlformats-officedocument.wordprocessingml.document.custom+xml";
            templatePackage.DeletePart(uriForCustomXml);
            var newXmlPart = templatePackage.CreatePart(uriForCustomXml, partContentType);
            var xStream = newXmlPart.GetStream();
            customXml.Save(xStream);
            templatePackage.Flush();
            templatePackage.Close();
            documentContent.DocumentContents = new byte[templateStream.Length];
            templateStream.Read(documentContent.DocumentContents, 0, (int)templateStream.Length);

    After I do the last statement, I don't have the expected document content in the byte array.  I'd guess that there should be some way to do this and I'm missing something simple?

    Thanks for any help!
    Darrell
  • Please have a look at this

    http://blogs.msdn.com/ericwhite/archive/2008/12/10/working-with-in-memory-open-xml-documents.aspx

    HTH,
    Ankush
  • Thanks, this code appears to alter the main document part.

    That's not exactly what I'm trying to do; I have code that has altered the CustomXML portion of a package -- which I know works -- and want to get it back into the memory stream, then grab the contents of the memory stream. I'd like to continue to use the Package classes for generality, rather than WordprocessingDocument, for generality.

    As I say, I have this working with Stream, but don't understand the mechanics of doing it with MemoryStream apparently.

    Thanks
    Darrell
Page 1 of 1 (3 items)