Replacing in inner Text in open xml element?

坚强是说给别人听的谎言 提交于 2019-12-10 20:42:38

问题


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.Operations 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,


回答1:


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.




回答2:


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.




回答3:


i had same problem. my solution is first do the replacement and then cloning.




回答4:


You must use for loop instead of foreach loop and access to each element using it's indexer.




回答5:


Instead of replacing InnerText, you can replace InnerXML

elem.InnerXml = elem.InnerXml.Replace(innerxml, modified);

and then call

mainDocumentPart.Document.Save();


来源:https://stackoverflow.com/questions/6869861/replacing-in-inner-text-in-open-xml-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!