替换打开的 xml 元素中的内部文本?
我正在使用 open xml SDK 2.0,对此我很陌生。
我实际上在我的 word 2007 文档中创建了一个名为“hello.docx”的快速部分(包含内容控件)。现在我需要将快速部分复制到名为“hello.docx”的同一文档的其他位置。我非常感谢这篇文章 http:// www.techques.com/question/1-3448297/Replacing-Content-Controls-in-OpenXML 同样的事情也发布在堆栈溢出论坛上,对此我非常感谢:)...这篇文章只是删除了内容控件,但保留了内容控件中的内容。
在上面的链接的帮助下,我能够修改代码以克隆内容控件并附加到同一文档(我的代码的这一部分正在工作)。但我的内部文本有问题。虽然我替换了打开的 Xml 元素中的innerText,但它没有反映在文档中。
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 元素中的替换字符串未反映在文档中。任何帮助将不胜感激。
提前致谢,
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,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您的“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.
您可以替换 InnerXML,
然后调用,而不是替换 InnerText
Instead of replacing InnerText, you can replace InnerXML
and then call
这是一个相当老的问题,但只是想与使用 OpenXML 2.9 的人分享,因此我建议您删除该 XmlElement 的所有子元素并添加包含新内容的新段落,而不是替换 insideText 或 InnerXML 想法,例如:
或更新现有文本元素
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:
Or Update existing Text element
您需要调用
mainDocumentPart.Document.Save();
作为打开文件的using
语句末尾的最后一行。这将保存您在打开文档时对文档所做的任何更改。You need to call
mainDocumentPart.Document.Save();
as the last line at the end of yourusing
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.
您必须使用 for 循环而不是 foreach 循环,并使用其索引器访问每个元素。
You must use for loop instead of foreach loop and access to each element using it's indexer.