将其添加到从MemoryStream打开的WordprocessingDocument中,而不会出现"Memory stream is not expandable"? [英] Adding to WordprocessingDocument opened from MemoryStream without getting "Memory stream is not expandable"?

查看:464
本文介绍了将其添加到从MemoryStream打开的WordprocessingDocument中,而不会出现"Memory stream is not expandable"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Open XML SDK,当我到达FeedData(msData)行时,以下内容显示内存流不可扩展":

Using Open XML SDK, the following gives "Memory stream is not expandable" when I reach the line FeedData(msData):

// Bytes in, bytes out
internal static byte[] UpdateDataStoreInMemoryStream(byte[] bytes,
   XmlDocument xdocData)
{
   using (var msDoc = new MemoryStream(bytes))
   {
      using (WordprocessingDocument wd = WordprocessingDocument.Open(msDoc, true))
      {
         MainDocumentPart mdp = wd.MainDocumentPart;
         CustomXmlPart cxp = mdp.CustomXmlParts.SingleOrDefault<CustomXmlPart>();
         using (MemoryStream msData = new MemoryStream())
         {
            xdocData.Save(msData);
            msData.Position = 0;
            // Replace content of ...\customXml\item1.xml. 
            cxp.FeedData(msData);
            // "Memory stream is not expandable" if more data than was there initially.
         }
      }
      return msDoc.ToArray();
   }
}

注意:麻烦不是msData,而是msDoc.

Note: it is not msData that is the trouble but msDoc.

Stein-Tore

Stein-Tore

推荐答案

问题是(实际上从错误消息中很明显)是

The trouble was (actually quite obvious from the error message) that

using (var msDoc = new MemoryStream(bytes)) ...

创建一个固定大小的MemoryStream. 因此,解决方案是创建一个可扩展的MemoryStream:

creates a fixed size MemoryStream. So solution is to create an expandable MemoryStream:

MemoryStream msDoc = new MemoryStream();
msDoc.Write(bytes, 0, bytes.Length);
msDoc.Position = 0;
...

这篇关于将其添加到从MemoryStream打开的WordprocessingDocument中,而不会出现"Memory stream is not expandable"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆