I'm using the OpenXML SDK to programatically replace some <w:sdt/>
elements with chunks of OpenXML (WordProcessingML) markup.
For example, I have a paragraph with these contents:
<w:p>
<w:run><w:text> Text before </w:text></w:run>
<w:sdt><w:sdtPr> ...</w:sdtPr><w:sdtContent>...</w:sdtContent></w:sdt>
<w:run><w:text> Text after </w:text></w:run>
</w:p>
And a table with a structure like this:
<w:tbl>
<w:tblPr>...</w:tblPr>
<w:tblGrid> ... gridCol elements ...</w:tblGrid>
<w:tr>
<w:trPr>...</w:trPr>
<w:tc>
<w:tcPr>...</w:tcPr>
<w:p> Cell contents </w:p>
</w:tc>
<w:tc>
<w:tcPr>...</w:tcPr>
<w:p> Cell contents </w:p>
</w:tc>
</w:tr>
</w:tbl>
Basically I want to replace the <w:sdt/>
element in the paragraph with the table markup.
The problem is that I cannot just replace it because it will create an invalid document (a table with paragraphs inside another paragraph element isn't valid).
The result that I want to get is this:
<w:p>
<w:run><w:text> Text before </w:text></w:run>
</w:p>
<w:tbl>
<w:tblPr>...</w:tblPr>
<w:tblGrid> ... gridCol elements ...</w:tblGrid>
<w:tr>
<w:trPr>...</w:trPr>
<w:tc>
<w:tcPr>...</w:tcPr>
<w:p> Cell contents </w:p>
</w:tc>
<w:tc>
<w:tcPr>...</w:tcPr>
<w:p> Cell contents </w:p>
</w:tc>
</w:tr>
</w:tbl>
<w:p>
<w:run><w:text> Text after </w:text></w:run>
</w:p>
I was thinking on creating a method that created a new paragraph with the elements before the <w:sdt/>
element, and another one with the elements after the sdt element, but it seems like such a method would be error-prone.
Is there a method that can do what I want without having to do it manually (a method that does what Word does when I try to insert a new table inside a text line)?
Any help will be greatly appreciated.