c# - How to replace text in XML document with special chars? -


look @ end of post addition problem textboxes!

with method want open document, replace text , leave alone. works, thats proud of. :d

public static void replaceinopenxmldocument(string pfad, string zuersetzen, string neuerstring)         {             using (wordprocessingdocument doc = wordprocessingdocument.open(pfad, true))             {                 var res = bm in doc.maindocumentpart.document.body.descendants()                           bm.innertext != string.empty && bm.innertext.contains(zuersetzen) && bm.haschildren == false                           select bm;                  foreach (var item in res)                 {                     item.insertafterself(new text(item.innertext.replace(zuersetzen, neuerstring)));                     item.remove();                 }                 doc.close();             }         } 

but works on replacing without special characters. example:

os replaced windows on 9000

[os] left is.

case 1:

in document:

you use os whatever purpose you've got.

replaceinopenxmldocument("c:\nsa\suspects.docx", "os", "win 2000"); 

will result in this:

you use win 2000 whatever purpose you've got.

case 2:

with special chars ...

you use [os] whatever purpose you've got.

replaceinopenxmldocument("c:\nsa\suspects.docx", "[os]", "win 2000"); 

... ignores me:

you use [os] whatever purpose you've got.

i tried several special characters ()[]{} etc., they're never replaced.

is there forgot do? or not able replace special characters method? if so, need simple workaround.

is there out desperation? :)

solution / addition 1:

thanks flowerking that! code i'm using right now:

public static void replaceinopenxmldocument(string pfad, string zuersetzen, string neuerstring)         {             using (wordprocessingdocument doc = wordprocessingdocument.open(pfad, true))             {                 simplifymarkupsettings settings = new simplifymarkupsettings                 {                     normalizexml = true, // merges run's in paragraph similar formatting                  };                 markupsimplifier.simplifymarkup(doc, settings);                  //zuersetzen = new xelement("name", zuersetzen).value;                 var res = bm in doc.maindocumentpart.document.body.descendants()                           bm.innertext != string.empty && bm.innertext.contains(zuersetzen) && bm.haschildren == false                           select bm;                 // bm.innertext.contains(zuersetzen)                  foreach (var item in res)                 {                     item.insertafterself(new text(item.innertext.replace(zuersetzen, neuerstring)));                     item.remove();                 }                  doc.close();             }         } 

(this code work normal documents normal text in it!)

solution / addition 2: if want replace text in textboxes, had little workaround. textboxes declared pictures, code above won't touch it.

i found additional class (link) searches through textboxes. zip-download includes exmaple program, easy understand.

this happening because open xml word creates when text contains special characters might :

  <w:r w:rsidrpr="00316587">     <w:rpr>       <w:rfonts w:ascii="consolas" w:hansi="consolas" w:eastasia="times new roman" w:cs="consolas" />       <w:color w:val="823125" />       <w:sz w:val="20" />       <w:szcs w:val="20" />       <w:lang w:eastasia="en-gb" />     </w:rpr>     <w:t>[</w:t>   </w:r>   <w:prooferr w:type="gramstart" />   <w:r w:rsidrpr="00316587">     <w:rpr>       <w:rfonts w:ascii="consolas" w:hansi="consolas" w:eastasia="times new roman" w:cs="consolas" />       <w:color w:val="823125" />       <w:sz w:val="20" />       <w:szcs w:val="20" />       <w:lang w:eastasia="en-gb" />     </w:rpr>     <w:t>text-to-replace</w:t>   </w:r>   <w:prooferr w:type="gramend" />   <w:r w:rsidrpr="00316587">     <w:rpr>       <w:rfonts w:ascii="consolas" w:hansi="consolas" w:eastasia="times new roman" w:cs="consolas" />       <w:color w:val="823125" />       <w:sz w:val="20" />       <w:szcs w:val="20" />       <w:lang w:eastasia="en-gb" />     </w:rpr>     <w:t>]</w:t>   </w:r> </w:p> 

the above shows open xml created text [text-to-replace]. (please note might not case, may depends on client using).

by looks of code doc.maindocumentpart.document.body.descendants() taking openxmlpart type descendants whole body of document , trying replace text iterating on one-by-one leaves actual text in 1 part , special characters in 2 sperate parts. hence code fails acheieve required.

there might different ways workaround this.

solution:

a nice (my preferred) solution normalize xml using markup simplifier openxml powertools, normalize open xml markup concatenate text in paragraph simplify working programatically.

example code:

using (wordprocessingdocument doc =             wordprocessingdocument.open("test.docx", true))  {       simplifymarkupsettings settings = new simplifymarkupsettings       {              normalizexml = true, // merges run's in paragraph similar formatting         };         markupsimplifier.simplifymarkup(doc, settings);   } 

please refer answer here more info on using markupsimplifier

hope helps :)


Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -