在打开的xml元素中替换内部Text? [英] Replacing in inner Text in open xml element?

查看:135
本文介绍了在打开的xml元素中替换内部Text?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用开放的xml SDK 2.0,对此我还是很陌生的.

I'm using open xml SDK 2.0 and i'm kind off new to this.

我实际上已经在我的Word 2007文档"hello.docx"中创建了一个quickpart(包含内容控件).现在,我需要将quickpart复制到名为"hello.docx"的同一文档的其他位置.我非常感谢这篇帖子 http://www.techques.com/question/1-3448297/Replacing-Content-Controls-in-OpenXML ,同样的事情在堆栈溢出论坛上发布,对此我非常感谢::...内容控件,但将内容保留在内容控件中.

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.

借助以上链接,我能够修改代码以克隆内容控件并将其追加到同一文档中(我的代码的这一部分正在工作).但是我在innerText中有问题.尽管我在打开的Xml元素中替换了innerText,但并没有反映在文件中.

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();
    }
}

openXML元素中的Replaced字符串未反映在文档中.任何帮助将不胜感激.

The Replaced string in the openXML element is not geting reflected in the document. Any help would be really appreciated.

预先感谢

推荐答案

您的"string replace ="行不执行任何操作.通过创建一个变量,您似乎知道string.Replace()并不是就地替换,但是您对该变量没有做任何事情.

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.

这篇关于在打开的xml元素中替换内部Text?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆