title: OpenXml Table error "<p> elements are required before every </tc>"
body: <p>I have created a Word template that I am then processing via the OpenXML SDK to replace some of the content of the document with data from a database query.</p>
  
  <p>The template consists of some basic text with Plain Text Content controls injected in the places that I want to replace the text. I am then using the text in these controls as a key to lookup the replacement values. In most cases, this is working fine (I simply update the Text property of the Text object).</p>
  
  <p>In one case I am replacing the text with a table. In this case, I build up a table in code and then I replace the content of the SdtContentRun object (the parent of the Run object, which in turn is the parent of the Text object) with the new Table object...</p>
  
  <pre><code>var sdtContentRunElements =
    from sdtContentRun in this.Document.MainDocumentPart.RootElement.Descendants&lt;SdtContentRun&gt;()
    select sdtContentRun;
  
  sdtContentRunElements.ForEach(sdtContentRunElement =&gt; {
  
    Run firstRunElement = sdtContentRunElement.Descendants&lt;Run&gt;().FirstOrDefault();
    if (firstRunElement != null) {
  
      Text firstTextElement = firstRunElement.Descendants&lt;Text&gt;().FirstOrDefault();
      if (firstTextElement != null) {
  
        switch (firstTextElement.Text) {
  
          case TableBookmark:
  
            Table advisoryTable = new Table(...); // See below
            OpenXmlElement parent = firstRunElement.Parent;
            parent.RemoveAllChildren();
            parent.Append(advisoryTable);
            break;
  
          case ContractorItemAdvisoriesLetter.ContractorCodeBookmark:
  
            firstTextElement.Text = @"New text";
            break;
  
        }
      }
    }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>This results in the following XML (taken from the Open XML SDK 2.0 Productivity Tool for Microsoft Office)...</p>
  
  <pre><code>&lt;w:sdtContent xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"&gt;
    &lt;w:tbl&gt;
      &lt;w:tr&gt;
        &lt;w:tc&gt;
          &lt;w:p&gt;
            &lt;w:r&gt;
              &lt;w:t&gt;Lorem ipsum dolor sit amet&lt;/w:t&gt;
            &lt;/w:r&gt;
          &lt;/w:p&gt;
        &lt;/w:tc&gt;
      &lt;/w:tr&gt;
    &lt;/w:tbl&gt;
  &lt;/w:sdtContent&gt;
  </code></pre>
  
  <p>(Some of the content of the table has been removed as I felt it would simply cloud the issue)</p>
  
  <p>When I then try to open the document in Word, I get an error. The error being reported is...</p>
  
  <blockquote>
    <p>Ambuguous cell mapping encountered. Possible missing paragraph
    element. &lt;p&gt; elements are required before every &lt;/tc&gt;</p>
  </blockquote>
  
  <p>It is probably worth mentioning that when I view the document explorer in Open XML SDK 2.0 Productivity Tool for Microsoft Office, the w:tbl element (and all elements contained within) are being recognised as <code>OpenXmlUnknownElement</code> rather than being recognised as <code>Table</code> objects. I do not know if this is relevant or whether it is a quirk of the SDK tool.</p>
  
  <p>Clearly I am missing something. As far as I can tell from the <a href="http://msdn.microsoft.com/en-us/library/documentformat.openxml.wordprocessing.table.aspx" rel="noreferrer">Table class definition</a>, it is perfectly legal to place a w:tbl inside a w:sdtContent (unless I am reading this incorrectly) so I am now at a loss as to what the problem may be. There are also very few results if you google "Table" and "OpenXmlUnknownElement" with none of the results appearing to be related to my problem.</p>
  
  <p>Any suggestions?</p>
  
  <p>EDIT: Further investigation seems to indicate that the problem actually lies in the embedding of another table within one of the cells of the table being added above. The new Table object is being added directly to a TableCell object which again, according to the documentation for Table referenced above, should be acceptable...</p>
  
  <pre><code>Table advisoryTable = new Table();
  
  advisories.ForEach(advisory =&gt; {
  
      advisoryTable.Append(new TableRow(new TableCell(new Paragraph(new Run(new Text(advisory.NoteText))))));
  
      advisory.ItemAdvisories.ForEach(itemAdvisory =&gt; {
  
          Item item = itemAdvisory.Item;
  
          Table itemTable = new Table();
          itemTable.Append(new TableRow[] {
  
              new TableRow(new TableCell[] {
                  new TableCell(new Paragraph(new Run(new Text(string.Format(@"Item {0}", item.Sequence)))))
              })
  
          });
  
          advisoryTable.Append(new TableRow(new TableCell(itemTable)));
  
      });
  
  });
  </code></pre>
  
  <p>Resulting in...</p>
  
  <pre><code>&lt;w:tbl xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"&gt;
    &lt;w:tr&gt;
      &lt;w:tc&gt;
        &lt;w:p&gt;
          &lt;w:r&gt;
            &lt;w:t&gt;Lorem ipsum dolor sit amet&lt;/w:t&gt;
          &lt;/w:r&gt;
        &lt;/w:p&gt;
      &lt;/w:tc&gt;
    &lt;/w:tr&gt;
    &lt;w:tr&gt;
      &lt;w:tc&gt;
        &lt;w:tbl&gt;
          &lt;w:tr&gt;
            &lt;w:tc&gt;
              &lt;w:p&gt;
                &lt;w:r&gt;
                  &lt;w:t&gt;Item 1&lt;/w:t&gt;
                &lt;/w:r&gt;
              &lt;/w:p&gt;
            &lt;/w:tc&gt;
          &lt;/w:tr&gt;
        &lt;/w:tbl&gt;
      &lt;/w:tc&gt;
    &lt;/w:tr&gt;
  &lt;/w:tbl&gt;
  </code></pre>
  
  <p>I have tried adding a Paragraph between the TableCell and the embedded Table, but this simply results in a new error, this time "unspecified error".</p>