I am attempting to modify innerxml of paragraph of a word document but the innerxml property refuses to be set. Following is the code I have tried:
static void Main(string[] args)
{
string destinationWordFile = @"C:\Users\Testing\Modified Files\1216085_01012013.docx";
using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(destinationWordFile, true, new OpenSettings { AutoSave = true }))
{
OpenXmlWdProcessing.RunProperties rp = new OpenXmlWdProcessing.RunProperties();
MainDocumentPart mainPart = wordDoc.MainDocumentPart;
var invoiceDocument = wordDoc.MainDocumentPart.Document;
var po = (from body in invoiceDocument.Body
where body.InnerText.Contains("PO:")
select body).FirstOrDefault();
string poInnerXml = po.InnerXml;
string modifiedXML = poInnerXml.Remove(poInnerXml.IndexOf("w:w=\""), 10);
po.InnerXml.Remove(0);
po.InnerXml.Insert(0,modifiedXML);
mainPart.Document.Save();
}
Following is the XML I am targetting:
<w:pPr xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:pStyle w:val="Normal" />
<w:framePr w:w="516" w:x="6881" w:y="7175" />
<w:widowControl w:val="off" />
<w:autoSpaceDE w:val="off" />
<w:autoSpaceDN w:val="off" />
<w:spacing w:before="0" w:after="0" w:line="179" w:lineRule="exact" />
<w:ind w:left="0" w:right="0" w:first-line="0" />
<w:jc w:val="left" />
<w:rPr>
<w:rFonts w:ascii="AATWWN+Helvetica" />
<w:color w:val="000000" />
<w:sz w:val="16" />
</w:rPr>
</w:pPr>
<w:r xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:rPr>
<w:rFonts w:ascii="AATWWN+Helvetica" />
<w:color w:val="000000" />
<w:sz w:val="16" />
</w:rPr>
<w:t>PO: 111111111111</w:t>
</w:r>
I want to remove w:w="516"
from <w:framePr w:w="516" w:x="6881" w:y="7175" />
.
Can anybody please advise me on this ? Many thanks in advance.