如何更换评论的InnerText [英] How to replace the InnerText of a Comment
问题描述
我试过以下内容:
comment.InnerText = comment.InnerText.Replace(comment.InnerText,新文本) ;
这是不行的,因为我们只能读取InnerText属性。我该如何有效改变的InnerText值,这样我就可以修改保存到 WordProcessing.CommentsPart.Comments
和 MainDocumentPart.Document
?
编辑: DocumentFormat.OpenXml.Wordprocessing.Comment
是注释的类
。
编辑2:方法:
公共无效updateCommentInnerTextNewWorkItem(列表<元组LT;的Int32,字符串,字符串> ;>名单){
//DOCX.CDOC.Comments - > WordProcessingCommentsPart.Comments
//DOCX._CIT - >字典< INT,串>
的foreach(在DOCX.CDOC.Comments VAR COMM)
{
的foreach(在列表VAR项)
{
的foreach(VAR item_cit在DOCX ._CIT)
{
如果(((评论)COMM).InnerText.Contains(<标签>中)及和放大器; item.Item3.Contains(item_cit.Value))
{
comm.InnerXml = comm.InnerXml.Replace(comm.InnerText,item.Item1 +);
//comm.InnerText.Replace(comm.InnerText,item.Item1+);
//DOCX.CDOC.Comments.Save();
//DOCX.DOC.MainDocumentPart.Document.Save();
}
如果(((评论)COMM).InnerText.Contains(<标签类)及和放大器; item.Item3.Contains(item_cit.Value))
{
//comm.InnerText.Replace(comm.InnerText,item.Item1 +);
comm.InnerXml = comm.InnerXml.Replace(comm.InnerText,item.Item1 +);
//DOCX.CDOC.Comments.Save();
//DOCX.DOC.MainDocumentPart.Document.Save();
}
}
}
}
DOCX.CDOC.Comments.Save();
DOCX.DOC.MainDocumentPart.Document.Save();
}
有没有这么容易的(但仍然并不复杂)。评论具有结构以及文档的身体 - 它可以包含段落,运行等的InnerText将只返回在此评论的所有段落中所有运行的您文本值,所以现在你明白为什么你不能只是设置此值。 p>
所以,首先你必须删除所有注释的段落:
comment.RemoveAllChildren< ;第>();
接下来的一步是用一个包含所需文本运行添加新的段落:
逐段=新的第();
RUN RUN =新的运行();
文本文本=新文本();
text.Text =我的意见;
run.Append(文本);
paragraph.Append(运行);
comment.Append(段落);
毕竟不要忘记保存更改:
doc.MainDocumentPart.WordprocessingCommentsPart.Comments.Save();
I've tried the following:
comment.InnerText=comment.InnerText.Replace(comment.InnerText,new_text);
Which doesn't work because we can only read the InnerText property. How do I effectively change the InnerText value so I can save the modifications to WordProcessing.CommentsPart.Comments
and MainDocumentPart.Document
?
EDIT: DocumentFormat.OpenXml.Wordprocessing.Comment
is comment's class.
EDIT 2: The method:
public void updateCommentInnerTextNewWorkItem(List<Tuple<Int32, String, String>> list){
//DOCX.CDOC.Comments -> WordProcessingCommentsPart.Comments
//DOCX._CIT -> Dictionary<int,string>
foreach (var comm in DOCX.CDOC.Comments)
{
foreach (var item in list)
{
foreach (var item_cit in DOCX._CIT)
{
if (((Comment)comm).InnerText.Contains("<tag>") && item.Item3.Contains(item_cit.Value))
{
comm.InnerXml = comm.InnerXml.Replace(comm.InnerText, item.Item1 + "");
//comm.InnerText.Replace(comm.InnerText,item.Item1+"");
//DOCX.CDOC.Comments.Save();
//DOCX.DOC.MainDocumentPart.Document.Save();
}
if (((Comment)comm).InnerText.Contains("<tag class") && item.Item3.Contains(item_cit.Value))
{
//comm.InnerText.Replace(comm.InnerText, item.Item1 + "");
comm.InnerXml = comm.InnerXml.Replace(comm.InnerText, item.Item1 + "");
//DOCX.CDOC.Comments.Save();
//DOCX.DOC.MainDocumentPart.Document.Save();
}
}
}
}
DOCX.CDOC.Comments.Save();
DOCX.DOC.MainDocumentPart.Document.Save();
}
It is not such easy(but still not complex). Comment has structure as well as document's body - it could contain Paragraphs, Runs etc. InnerText will just return to you text values of all runs of all paragraphs in this comment, so now you understand why you can not just set this value.
So first you have to remove all comment's paragraphs:
comment.RemoveAllChildren<Paragraph>();
Next step is to add new paragraph with run that contains text you need:
Paragraph paragraph = new Paragraph();
Run run = new Run();
Text text = new Text();
text.Text = "My comment";
run.Append(text);
paragraph.Append(run);
comment.Append(paragraph);
After all do not forget to save changes:
doc.MainDocumentPart.WordprocessingCommentsPart.Comments.Save();
这篇关于如何更换评论的InnerText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!