Modifying a Word document template with OpenXML I have the following program that opens a Word document template using the OpenXML library and replaces a couple of phrases with their counterparts from what will be a database (right now its just dummy data). Something about the if-else struture in the nested  foreach loops bothers me. Is there a better way I can accomplish this task? Would regular expressions be a more viable option?

using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using System;

namespace OpenXmlSearchExample
{
    class Program
    {
        static void Main(string[] args)
        {
            string templatePath = @"C:\users\example\desktop\template.dotx";
            string resultPath = @"C:\users\example\desktop\OpenXmlExample.docx";

            using (WordprocessingDocument document = WordprocessingDocument.CreateFromTemplate(templatePath))
            {
                var body = document.MainDocumentPart.Document.Body;
                var paragraphs = body.Elements&lt;Paragraph&gt;();

                // Iterate through paragraphs, runs, and text, finding the text we want and replacing it
                foreach (Paragraph paragraph in paragraphs)
                {
                    foreach (Run run in paragraph.Elements&lt;Run&gt;())
                    {
                        foreach (Text text in run.Elements&lt;Text&gt;())
                        {
                            if (text.Text == "Plan")
                            {
                                text.Text = string.Format("{0} {1} Plan", DateTime.Now.Year, "Q2");
                            }
                            else if (text.Text == "Project Name")
                            {
                                text.Text = "SUPER SECRET CODE NAME";
                            }
                            else if (text.Text == "WO-nnnn Name")
                            {
                                text.Text = "Maintenance";
                            }
                            else
                            {
                                Console.WriteLine(text.Text);
                                Console.ReadKey();
                            }
                        }
                    }
                }

                // Save result document, not modifying the template
                document.SaveAs(resultPath);
            }
        }
    }
}
Using case-sensitive string matching like that is in my opinion the first nail in the coffin. Instead a case-insensitive dictionary would be a much better option.

Example:

var replacements = new Dictionary&lt;string, Func&lt;string&gt;&gt;(StringComparer.OrdinalIgnoreCase)
{
    ["Plan"] = () =&gt; $"{DateTime.Now.Year} Q2 Plan",
    ["Project Name"] = () =&gt; $"SUPER SECRET CODE NAME",
};

text.Text = replacements[text.Text]();


This is also much easier to extend/maintain.



You shouldn't mix full type names with var. You can use var not only for normal variables but also inside using statements or foreach loops. Keep it consistent.