c# - Word, VSTO, OpenXml - Inserting XML into Paragraph object -


i using following code inject xml (openxml) range of paragraph in word. problem error message stating "xml markup cannot inserted specified location"

c# code:

try {         string oxml = ""; // contains xml listed below     // first paragraph     paragraph p = this.paragraphs[1];     object missing = type.missing;     // insert openxml formatted xml paragraph range     p.range.insertxml(oxml, ref missing);  // causes exception   } catch (exception ex) {     //_logger.error("openxml injection", ex); } 

xml injected:

<?xml version="1.0" encoding="utf-8" standalone="yes"?> <?mso-application progid="word.document"?> <pkg:package xmlns:pkg="http://schemas.microsoft.com/office/2006/xmlpackage" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">   <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">     <w:p>       <w:r>         <w:t xml:space="preserve">(a) costs , expenses</w:t>       </w:r>       <w:del w:id="33350" w:author="exemplify">         <w:r>           <w:deltext xml:space="preserve">. </w:deltext>         </w:r>       </w:del>       <w:r>         <w:t xml:space="preserve"> the</w:t>       </w:r>       <w:commentrangestart w:id="33351" />       <w:del w:id="33352" w:author="exemplify">         <w:r>           <w:deltext xml:space="preserve"> borrower</w:deltext>         </w:r>       </w:del>       <w:commentrangeend w:id="33351" />       <w:r>         <w:commentreference w:id="33351" />       </w:r>       <w:r>         <w:t xml:space="preserve"> shall pay (i) reasonable out-of-pocket expenses incurred administrative agent , affiliates</w:t>       </w:r>       <w:del w:id="33353" w:author="exemplify">         <w:r>           <w:deltext xml:space="preserve">, </w:deltext>         </w:r>       </w:del>       <w:ins w:id="33354" w:author="exemplify">         <w:r>           <w:t xml:space="preserve"> (</w:t>         </w:r>       </w:ins>       <w:r>         <w:t xml:space="preserve">.</w:t>       </w:r>     </w:p>   </w:document> </pkg:package> 

to use insertxml, xml either has complete, valid word 2003 wordprocessingml document, or complete, valid open office xml document in flat opc format. @ moment xml have isn't either of those.

to valid flat opc package, package has contain parts, , define necessary relationship parts.

to valid word 2003 xml document, namespace declaration needs different, document element needs

<w:worddocument> 

and inside need

<w:body> 

element. suspect of elements have not valid xml vocabulary (you can check yourself) example following xml elements , attributes removed should inject ok:

<?xml version="1.0" encoding="utf-8" standalone="yes"?> <?mso-application progid="word.document"?> <w:worddocument xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml">   <w:body>     <w:p>       <w:r>         <w:t>(a) costs , expenses</w:t>       </w:r>       <w:r>         <w:t> the</w:t>       </w:r>       <w:r>       </w:r>       <w:r>         <w:t> shall pay (i) reasonable out-of-pocket expenses incurred administrative agent , affiliates</w:t>       </w:r>       <w:r>         <w:t>.</w:t>       </w:r>     </w:p>   </w:body> </w:worddocument> 

for word 2007 , later xml, needs inside part within package, , package has define relationships. in case, following (you need namespace defintions referenced in xml):

<?xml version="1.0" encoding="utf-8" standalone="yes"?> <?mso-application progid="word.document"?> <pkg:package xmlns:pkg="http://schemas.microsoft.com/office/2006/xmlpackage">   <pkg:part pkg:name="/_rels/.rels" pkg:contenttype="application/vnd.openxmlformats-package.relationships+xml">     <pkg:xmldata>       <relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">         <relationship id = "rid1" type="http://schemas.openxmlformats.org/officedocument/2006/relationships/officedocument" target="word/document.xml" />       </relationships>     </pkg:xmldata>   </pkg:part>   <pkg:part pkg:name="/word/document.xml" pkg:contenttype="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml">     <pkg:xmldata>       <w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">         <w:body>           <w:p>             <w:r>               <w:t>(a) costs , expenses</w:t>             </w:r>             <w:del w:id="33350" w:author="exemplify">               <w:r>                 <w:deltext>. </w:deltext>               </w:r>             </w:del>             <w:r>               <w:t> the</w:t>             </w:r>             <w:commentrangestart w:id="33351" />             <w:del w:id="33352" w:author="exemplify">               <w:r>                 <w:deltext> borrower</w:deltext>               </w:r>             </w:del>             <w:commentrangeend w:id="33351" />             <w:r>               <w:t> shall pay (i) reasonable out-of-pocket expenses incurred administrative agent , affiliates</w:t>             </w:r>             <w:del w:id="33353" w:author="exemplify">               <w:r>                 <w:deltext>, </w:deltext>               </w:r>             </w:del>             <w:ins w:id="33354" w:author="exemplify">               <w:r>                 <w:t> (</w:t>               </w:r>             </w:ins>             <w:r>               <w:t>.</w:t>             </w:r>           </w:p>         </w:body>       </w:document>     </pkg:xmldata>   </pkg:part> </pkg:package> 

i have omitted xml:space="preserve" attributes - may need them. have omitted following chunk

            <w:r>               <w:commentreference w:id="33351" />             </w:r> 

which cause failure. guess it's because references comment id doesn't exist.


Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -