title: Search and replace placeholders split up to multiple <w:t>-Elements
body: <p>I´m trying to create reports from .docx-Templates using the Open XML SDK 2.5. Within the templates I have defined some placeholders that get replaced by real values. The placeholders can be defined in various schemas such as</p>
  
  <pre><code>&lt;#Name#&gt;
  or
  &lt;!#Name#!&gt;
  or
  #Name#
  or
  {{Name}}
  </code></pre>
  
  <p>The schema of the placeholder can also be in another format, as long as the placeholders can be clearly identified within the text.</p>
  
  <p>The problem I am currently facing is that a placeholder is often split among multiple <code>&lt;w:t&gt;</code>-Elements (<code>DocumentFormat.OpenXml.Wordprocessing.Text</code>) within an <code>&lt;w:p&gt;</code>-Element (<code>DocumentFormat.OpenXml.Wordprocessing.Paragraph</code>). An example</p>
  
  <pre><code>&lt;w:p w:rsidR="003137E0" w:rsidRDefault="008C62F1" w:rsidP="00D43D55"&gt;
    &lt;w:r&gt;
      &lt;w:t xml:space="preserve"&gt;#FirstName# &lt;/w:t&gt;
    &lt;/w:r&gt;
    &lt;w:r w:rsidR="00C93A70"&gt;
      &lt;w:t&gt;#LastName&lt;/w:t&gt;
    &lt;/w:r&gt;
    &lt;w:r w:rsidR="005F49B7"&gt;
      &lt;w:t&gt;#&lt;/w:t&gt;
    &lt;/w:r&gt;
  &lt;/w:p&gt;
  </code></pre>
  
  <p>Here the placeholder <code>#FirstName#</code> is easily identifyable, cause it is within one <code>&lt;w:t&gt;</code>-Element, but the placeholder <code>#LastName#</code> is split among multiple <code>&lt;w:t&gt;</code>-Elements, so that I cannot use a simple Regex on the Text on the Document like</p>
  
  <pre><code>Regex placeholderRegex = new Regex(@"#[\w]*#");
  
  document.MainDocumentPart.Document.Body.Descendants&lt;Text&gt;().Where(t=&gt; placeholderRegex.IsMatch(t.Text))
  </code></pre>
  
  <p>I have no control how the templates get defined and I also will not put constraints on the Users how they have to create the template. For me it is also not clear when a placeholder gets split into multiple <code>&lt;w:t&gt;</code>-Elements.</p>
  
  <p>Another example using <code>{{[\w]*}}</code> as schema for placeholders.</p>
  
  <p><strong>Text (Docx)</strong></p>
  
  <pre><code>{{Ort}}
  And this {{placeholder}} is within the text 
  </code></pre>
  
  <p><strong>Xml (OpenXML)</strong></p>
  
  <pre><code>&lt;w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 wp14"&gt;
    &lt;w:body&gt;
      &lt;w:p w:rsidR="007B60F2" w:rsidRDefault="00BB7370" w:rsidP="00D43D55"&gt;
        &lt;w:pPr&gt;
          &lt;w:rPr&gt;
            &lt;w:lang w:val="en-US" /&gt;
          &lt;/w:rPr&gt;
        &lt;/w:pPr&gt;
        &lt;w:r w:rsidRPr="00114EA7"&gt;
          &lt;w:rPr&gt;
            &lt;w:lang w:val="en-US" /&gt;
          &lt;/w:rPr&gt;
          &lt;w:t&gt;{{&lt;/w:t&gt;
        &lt;/w:r&gt;
        &lt;w:r w:rsidR="00C93A70" w:rsidRPr="00114EA7"&gt;
          &lt;w:rPr&gt;
            &lt;w:lang w:val="en-US" /&gt;
          &lt;/w:rPr&gt;
          &lt;w:t&gt;Ort&lt;/w:t&gt;
        &lt;/w:r&gt;
        &lt;w:r w:rsidR="00114EA7" w:rsidRPr="00114EA7"&gt;
          &lt;w:rPr&gt;
            &lt;w:lang w:val="en-US" /&gt;
          &lt;/w:rPr&gt;
          &lt;w:t&gt;}}&lt;/w:t&gt;
        &lt;/w:r&gt;
      &lt;/w:p&gt;
      &lt;w:p w:rsidR="00EC3BED" w:rsidRPr="00114EA7" w:rsidRDefault="00C310E0" w:rsidP="00D43D55"&gt;
        &lt;w:pPr&gt;
          &lt;w:rPr&gt;
            &lt;w:lang w:val="en-US" /&gt;
          &lt;/w:rPr&gt;
        &lt;/w:pPr&gt;
        &lt;w:r w:rsidRPr="00114EA7"&gt;
          &lt;w:rPr&gt;
            &lt;w:lang w:val="en-US" /&gt;
          &lt;/w:rPr&gt;
          &lt;w:t xml:space="preserve"&gt;This is a text with a &lt;/w:t&gt;
        &lt;/w:r&gt;
        &lt;w:r w:rsidR="00A07A5D"&gt;
          &lt;w:rPr&gt;
            &lt;w:lang w:val="en-US" /&gt;
          &lt;/w:rPr&gt;
          &lt;w:t&gt;{{&lt;/w:t&gt;
        &lt;/w:r&gt;
        &lt;w:r w:rsidRPr="00114EA7"&gt;
          &lt;w:rPr&gt;
            &lt;w:lang w:val="en-US" /&gt;
          &lt;/w:rPr&gt;
          &lt;w:t&gt;placeholder&lt;/w:t&gt;
        &lt;/w:r&gt;
        &lt;w:r w:rsidR="00A07A5D"&gt;
          &lt;w:rPr&gt;
            &lt;w:lang w:val="en-US" /&gt;
          &lt;/w:rPr&gt;
          &lt;w:t&gt;}&lt;/w:t&gt;
        &lt;/w:r&gt;
        &lt;w:r w:rsidR="00114EA7" w:rsidRPr="00114EA7"&gt;
          &lt;w:rPr&gt;
            &lt;w:lang w:val="en-US" /&gt;
          &lt;/w:rPr&gt;
          &lt;w:t&gt;}&lt;/w:t&gt;
        &lt;/w:r&gt;
        &lt;w:bookmarkStart w:id="0" w:name="_GoBack" /&gt;
        &lt;w:bookmarkEnd w:id="0" /&gt;
        &lt;w:r w:rsidR="00A07A5D"&gt;
          &lt;w:rPr&gt;
            &lt;w:lang w:val="en-US" /&gt;
          &lt;/w:rPr&gt;
          &lt;w:t&gt;.&lt;/w:t&gt;
        &lt;/w:r&gt;
      &lt;/w:p&gt;
      &lt;w:sectPr w:rsidR="00EC3BED" w:rsidRPr="00114EA7" w:rsidSect="00237721"&gt;
        &lt;w:pgSz w:w="11906" w:h="16838" /&gt;
        &lt;w:pgMar w:top="1417" w:right="1417" w:bottom="1134" w:left="1417" w:header="708" w:footer="708" w:gutter="0" /&gt;
        &lt;w:cols w:space="708" /&gt;
        &lt;w:docGrid w:linePitch="360" /&gt;
      &lt;/w:sectPr&gt;
    &lt;/w:body&gt;
  &lt;/w:document&gt;
  </code></pre>
  
  <p>So my question is now whats the way to search and replace placeholders with values using Open XML SDK? Is there some functionality within the SDK that can help me? Has anybody else solved this problem and provide assistance?</p>