Replacing in inner Text in open xml element?
I'm using open xml SDK 2.0 and i'm kind off new to this.
I have actually created a quickpart (containg content control) in my word 2007 document named "hello.docx". Now I need to copy the quickpart into the other location of the same document named "hello.docx". I was very thank full for this post http://www.techques.com/question/1-3448297/Replacing-Content-Controls-in-OpenXML and same thing is posted on stack overflow forum for which i was very thank full :)...This post just deletes the content control but keeps the content in Content control.
With the help of the above link I was able to modify the code to clone the content control and append to the same document (This part of my code is working). But i have problem in innerText. Though i replace the innerText in the open Xml element, it is not geting reflected in the doucument.
public static void AddingSdtBlock(string filename, string sdtBlockTag)
{
using (WordprocessingDocument doc = WordprocessingDocument.Open(filename,true))
{
MainDocumentPart mainDocumentPart = doc.MainDocumentPart;
List<SdtBlock> sdtList = mainDocumentPart.Document.Descendants<SdtBlock>().ToList();
SdtBlock sdtA = null;
foreach (SdtBlock sdt in sdtList)
{
if (sdt.SdtProperties.GetFirstChild<Tag>().Val.Value == sdtBlockTag)
{
sdtA = sdt;
break;
}
}
SdtBlock cloneSdkt = (SdtBlock)sdtA.Clone();
OpenXmlElement sdtc = cloneSdkt.GetFirstChild<SdtContentBlock>();
// OpenXmlElement parent = cloneSdkt.Parent;
OpenXmlElementList elements = cloneSdkt.ChildElements;
// var mySdtc = new SdtContentBlock(cloneSdkt.OuterXml);
foreach (OpenXmlElement elem in elements)
{
string innerxml= elem.InnerText ;
if (innerxml.Length>0)
{
string modified = "Class Name : My Class.Description : mydesc.AttributesNameDescriptionMy Attri name.my attri desc.Op开发者_开发百科erations NameDescriptionmy ope name.my ope descriptn.";
string replace= elem.InnerText.Replace(innerxml, modified);
// mainDocumentPart.Document.Save();
}
// string text = parent.FirstChild.InnerText;
// parent.Append((OpenXmlElement)elem.Clone());
}
mainDocumentPart.Document.Body.AppendChild<SdtBlock>(cloneSdkt);
//sdtA.Remove();
}
}
The Replaced string in the openXML element is not geting reflected in the document. Any help would be really appreciated.
Thanks in advance,
Your "string replace= " line does nothing. By creating a variable you seem aware that string.Replace() is not an in-place replacement, but then you are not doing anything with the variable.
Instead of replacing InnerText, you can replace InnerXML
elem.InnerXml = elem.InnerXml.Replace(innerxml, modified);
and then call
mainDocumentPart.Document.Save();
This's a pretty old question but just want to share with guys using OpenXML 2.9, so that instead of replace innerText or InnerXML idea, I recommend you remove all child elements of that XmlElement and add a new paragraph with new content, for example:
var tableCell = mainPart.Document.Body.Elements<TableCell>().First();
tableCell.RemoveAllChildren();
tableCell.AppendChild<Paragraph>(new Paragraph(new Run(new Text("This is new text"))));
mainPart.Document.Save();
Or Update existing Text element
var tableCell = mainPart.Document.Body.Elements<TableCell>().First();
tableCell.Descendants<Text>().FirstOrDefault(e => e.Text == "This is old text");
if (text != null)
{
text.Text = "This is new text";
}
mainPart.Document.Save();
You need to call mainDocumentPart.Document.Save();
as the last line at the end of your using
statement that opens the file. This will save any changes you made to the document while it has been opened.
i had same problem. my solution is first do the replacement and then cloning.
You must use for loop instead of foreach loop and access to each element using it's indexer.
精彩评论