在打开的xml元素中替换内部Text?

编程入门 行业动态 更新时间:2024-10-27 12:41:00
本文介绍了在打开的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"的同一文档的其他位置.我非常感谢这篇帖子 www.techques/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 www.techques/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?

本文发布于:2023-10-24 09:55:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1523634.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:元素   xml   Text

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!