title: Insert multiple lines of text into a Rich Text content control with OpenXML
body: <p>I'm having difficulty getting a content control to follow multi-line formatting. It seems to interpret everything I'm giving it literally. I am new to OpenXML and I feel like I must be missing something simple.</p>
  
  <p>I am converting my multi-line string using this function.</p>
  
  <pre><code>    private static void parseTextForOpenXML(Run run, string text)
      {
          string[] newLineArray = { Environment.NewLine, "&lt;br/&gt;", "&lt;br /&gt;", "\r\n" };
          string[] textArray = text.Split(newLineArray, StringSplitOptions.None);
  
          bool first = true;
  
          foreach (string line in textArray)
          {
              if (!first)
              {
                  run.Append(new Break());
              }
  
              first = false;
  
              Text txt = new Text { Text = line };
              run.Append(txt);
          }
      }
  </code></pre>
  
  <p>I insert it into the control with this</p>
  
  <pre><code>    public static WordprocessingDocument InsertText(this WordprocessingDocument doc, string contentControlTag, string text)
      {
          SdtElement element = doc.MainDocumentPart.Document.Body.Descendants&lt;SdtElement&gt;().FirstOrDefault(sdt =&gt; sdt.SdtProperties.GetFirstChild&lt;Tag&gt;().Val == contentControlTag);
  
          if (element == null)
              throw new ArgumentException("ContentControlTag " + contentControlTag + " doesn't exist.");
  
          element.Descendants&lt;Text&gt;().First().Text = text;
          element.Descendants&lt;Text&gt;().Skip(1).ToList().ForEach(t =&gt; t.Remove());
  
          return doc;
      }
  </code></pre>
  
  <p>I call it with something like...</p>
  
  <pre><code>doc.InsertText("Primary", primaryRun.InnerText);
  </code></pre>
  
  <p>Although I've tried InnerXML and OuterXML as well. The results look something like</p>
  
  <p>Example AttnExample CompanyExample AddressNew York, NY 12345 or</p>
  
  <pre><code>&lt;w:r xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"&gt;&lt;w:t&gt;Example Attn&lt;/w:t&gt;&lt;w:br /&gt;&lt;w:t&gt;Example Company&lt;/w:t&gt;&lt;w:br /&gt;&lt;w:t&gt;Example Address&lt;/w:t&gt;&lt;w:br /&gt;&lt;w:t&gt;New York, NY 12345&lt;/w:t&gt;&lt;/w:r&gt;
  </code></pre>
  
  <p>The method works fine for simple text insertion. It's just when I need it to interpret the XML that it doesn't work for me.</p>
  
  <p>I feel like I must be super close to getting what I need, but my fiddling is getting me nowhere. Any thoughts? Thank you.</p>