0

I am using MSDN example code to replace a mergefield with some text. However if I have more than one mergefield with the same text, the code only affects one and not the others. How do I solve this?

This is the code I'm using

foreach (var field in mainPart.Document.Descendants<SimpleField>()){
    string fieldText = GetFieldName(field);
    if (fieldText.Equals(fldName))
    {
        string strText = null;
        foreach (Text objText in field.Descendants<Text>())
        {
            strText = objText.OuterXml;
            objText.Remove();
            break;
        }
        field.Parent.Append(new Run(new Text("Hello")));
        mainPart.Document.Save();
    }
}

I have 3 simplefields but it only replaces one.

Andrea
  • 11,801
  • 17
  • 65
  • 72
  • 1
    Your question is quite vague. If you want us to help you give us the code sample which is causing the problem. Explain what you think works and what you think doesn't etc. 2 lines of text is not good enough to have an idea of what could be your problem. Pretty much how stackoverflow works is : Show us what you have done (so we know what's wrong) and that you have tried ("give me the code I don't care why it works" kind of questions are usually not accepted too well here). If your question shows no effort no one will want to put effort in answering it – micbobo May 22 '15 at 17:29
  • http://stackoverflow.com/help/how-to-ask this should help you ask a better question – micbobo May 22 '15 at 17:36
  • @micbobo you can check now, I hope you can help me – Daniel Blanco Baines May 25 '15 at 15:34
  • 1
    They all have same fldName? – Rodrigo López May 25 '15 at 19:08
  • @RodrigoLópez Yes, they have – Daniel Blanco Baines Jul 02 '15 at 20:09

0 Answers0