.docx найти/заменить текст на /r/n в xml ⇐ C#
-
Anonymous
.docx найти/заменить текст на /r/n в xml
I'm basically opening the docx file taking the xml part and doing a string find/replace but the string I'm replacing with has /r/n in it. Whenever I do that I get the error "Memory stream is not expandable". If I remove the /r/n it works find but I obviously want to put in a break. I tried replacing them with but I get the same error.
This is done in a website and it downloads the result for the user.
try { var templatesPath = HttpContext.Current.Server.MapPath("~/Templates"); var filePath = System.IO.Path.Combine(templatesPath, docName); MemoryStream originalStream = new MemoryStream(File.ReadAllBytes(filePath)); using (WordprocessingDocument doc = WordprocessingDocument.Open(originalStream, true)) { MainDocumentPart mainPart = doc.MainDocumentPart; StreamReader streamReader = new StreamReader(mainPart.GetStream()); string docText = streamReader.ReadToEnd(); var updatedDocText = MassiveReplace(docText, Id); StreamWriter streamWriter = new StreamWriter(mainPart.GetStream(FileMode.Create, FileAccess.Write)); streamWriter.Write(updatedDocText); streamWriter.Flush(); } var response = new HttpResponseMessage(HttpStatusCode.OK); response.Content = new ByteArrayContent(originalStream.ToArray()); response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment") { FileName = docName }; response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.wordprocessingml.document"); return response; } catch (Exception ex) { throw; }
Источник: https://stackoverflow.com/questions/780 ... on-the-xml
I'm basically opening the docx file taking the xml part and doing a string find/replace but the string I'm replacing with has /r/n in it. Whenever I do that I get the error "Memory stream is not expandable". If I remove the /r/n it works find but I obviously want to put in a break. I tried replacing them with but I get the same error.
This is done in a website and it downloads the result for the user.
try { var templatesPath = HttpContext.Current.Server.MapPath("~/Templates"); var filePath = System.IO.Path.Combine(templatesPath, docName); MemoryStream originalStream = new MemoryStream(File.ReadAllBytes(filePath)); using (WordprocessingDocument doc = WordprocessingDocument.Open(originalStream, true)) { MainDocumentPart mainPart = doc.MainDocumentPart; StreamReader streamReader = new StreamReader(mainPart.GetStream()); string docText = streamReader.ReadToEnd(); var updatedDocText = MassiveReplace(docText, Id); StreamWriter streamWriter = new StreamWriter(mainPart.GetStream(FileMode.Create, FileAccess.Write)); streamWriter.Write(updatedDocText); streamWriter.Flush(); } var response = new HttpResponseMessage(HttpStatusCode.OK); response.Content = new ByteArrayContent(originalStream.ToArray()); response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment") { FileName = docName }; response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.wordprocessingml.document"); return response; } catch (Exception ex) { throw; }
Источник: https://stackoverflow.com/questions/780 ... on-the-xml
Мобильная версия