none
Accessing the Stream Open XML is using RRS feed

  • Question

  • Hi;

    We need to use the Stream that an Open XML document is using. It is always a MemoryStream. Can we use the existing MemoryStream (setting it's position to 0 and then reading through to the end) or do we need to copy it to a new MemoryStream?

    Also, do we need to call anything to flush everything in the Open XML classes to the underlying Stream - or is that done on each call?

    thanks - dave
    Windward Reports - World's Greatest SharePoint Reporting & DocGen
    Thursday, January 21, 2010 12:26 AM

Answers

  • Hi DavidThi808,

    Thanks for your description.

    In your case, if you make some changes to doc, there will not be any changes except the "Position" property (which will be changed when loading OpenXmlPart) to the stream buf before calling .Save() or .Close() (if auto save is on). But if you make changes to buf in another method, doc may be affected when loading OpenXmlPart.

    In particular, if you set isEditable = false or the stream readonly, there won't be any changes.

    BTW, it is not the only way to open a package by stream, you could also try other overloaded WordprocessingDocument.Open() methods using package or filepath(string) as input, in that way the stream is not shared in your case.

    Hope this helps. If you have any questions, please let me know.

    Thanks,

    Lu
    Thursday, January 21, 2010 5:05 AM

All replies

  • Hi DavidThi808,

    Thanks for your question.

    What do you mean by "the Stream that an Open XML document is using"? Are you using OpenXmlPart.FeedData() or other methods using Stream as an input? Could you describe more about it?

    Thanks,

    Lu
    Thursday, January 21, 2010 2:32 AM
  • Hi;

    If I do:
    // byte[] fileImage is a copy of a docx file.
       MemoryStream buf = new MemoryStream(fileImage);
       WordprocessingDocument doc = WordprocessingDocument.Open(buf, true);
    ... make some changes to doc ...

    Can I now use buf to pass to a method in our code that needs to scan the DOCX file? And is it ok if that code changes the Position member of buf?

    thanks - dave

    Windward Reports - World's Greatest SharePoint Reporting & DocGen
    Thursday, January 21, 2010 3:26 AM
  • Hi DavidThi808,

    Thanks for your description.

    In your case, if you make some changes to doc, there will not be any changes except the "Position" property (which will be changed when loading OpenXmlPart) to the stream buf before calling .Save() or .Close() (if auto save is on). But if you make changes to buf in another method, doc may be affected when loading OpenXmlPart.

    In particular, if you set isEditable = false or the stream readonly, there won't be any changes.

    BTW, it is not the only way to open a package by stream, you could also try other overloaded WordprocessingDocument.Open() methods using package or filepath(string) as input, in that way the stream is not shared in your case.

    Hope this helps. If you have any questions, please let me know.

    Thanks,

    Lu
    Thursday, January 21, 2010 5:05 AM
  • Hello Dave,

    If you have any further questions or concerns, please feel free to unmart and update here. :-)


    Best regards,
    Ji Zhou
    MSDN Subscriber Support in Forum
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Friday, January 29, 2010 1:27 AM
    Moderator
  • Hi;

    I think the correct answer to this is no, you can't do it. It is covered in better detail in this thread - http://social.msdn.microsoft.com/Forums/en-US/oxmlsdk/thread/1f849681-b6d3-4c65-b84f-663f14244c55

    thanks - dave
    Windward Reports - World's Greatest SharePoint Reporting & DocGen
    Friday, January 29, 2010 3:51 PM