0

I have one .docx file and Dictionary<string,string> . I need to go through the document, and replace the text eqaul to Dictionary.Key to its according value in Dictionary. It will be on server, because of that I don't want to use Microsoft.Office.Interopt library which generates COM objects. For the solution i prefered OpenXML, but on web for me there is no working solution for it. I found this solution of Eric White . It didn't work for me. Exactly to say, I run method SearchAndReplace but there is no changes in document. I went through the methods, and found the problem was in method PutXmlInDocument method, which saves XmlDocument to given OpenXmlPart by getting stream of it. Is there any other solutions with using OpenXML or what to do save XmlDocument to OpenXmlPart?

It is that method which saving was unsuccessful :

public static void PutXmlDocument(OpenXmlPart part, XmlDocument xmlDoc)
    {
        using (Stream partStream = part.GetStream(FileMode.Create, FileAccess.Write))
        using (XmlWriter partXmlWriter = XmlWriter.Create(partStream))
            xmlDoc.Save(partXmlWriter);

    }
Javidan
  • 566
  • 1
  • 9
  • 25
  • 1
    http://www.codeproject.com/Articles/87711/Manipulate-Docx-with-C-without-Microsoft-Word-inst – Sam Leach Apr 26 '13 at 14:00
  • Is it in a paragraph? Is it in a table? Is it in the header? Is it in the footer? How many values of these are in the docx? If there are more than one, do you want to replace all of them or just the first one? We need a lot more info. – jn1kk Apr 26 '13 at 14:05
  • I have no info about that. I have a list of "search values" and a list of "new values" . I need to search "search value" and change it to "new value" . It can be each of you wrote above. – Javidan Apr 26 '13 at 14:10
  • @Javidan, how do you not know? Your answer to this question makes my answer vary by up to 100-200 lines. Can you not make any assumptions? – jn1kk Apr 26 '13 at 14:17
  • I can only say that, it cannot be in header and in footer. – Javidan Apr 26 '13 at 14:20
  • As in `Microsoft.Office.Interop.Word`, I know there a method of `Replace` . I need something like it – Javidan Apr 26 '13 at 14:22
  • I found this [solution of Eric White](http://openxmldeveloper.org/blog/b/openxmldeveloper/archive/2011/05/12/148357.aspx) ,but it doesn't work for me. – Javidan Apr 26 '13 at 14:29
  • If you guys are able to, could you help out with this? Thanks! http://stackoverflow.com/questions/26307691 – slayernoah Oct 10 '14 at 22:03

1 Answers1

0

I contacted to blogger, and asked about this situation. At last I reach to this solution. If replacing is 1 per WordprocessingDocument then Eric White's solution works great. But if it is more than 1, then after compliting all replacements the below method must be called before doing something with WordprocessingDocument.

WordprocessingDocument.Package.Close();
Javidan
  • 566
  • 1
  • 9
  • 25