How to replace the InnerText of a Comment

牧云@^-^@ 提交于 2019-12-23 03:38:31

问题


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

    }

回答1:


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



回答2:


It's read-only because it returns the XML content with all XML tags removed. So setting it would strip it of all XML tags.

If the text you want to replace does not span tags you could just replace the text in the XML:

comment.InnerXml=comment.InnerXml.Replace(comment.InnerText,new_text);



回答3:


Ahh....This is a little complex.And I have ever had the same problem.

You will need the XmlElement Class.And for example, there is a variable named xmlDoc which has been instantiated from XmlDocument. And then you should use the method SelectSingleNode to get the reference of which XmlNode you want to edit.Here you need to transform the XmlNode into XmlElement by using this(Suppose the XmlNode is named 'node'):

XmlElement XmlEle = (XmlElement)node;

Also in easy way, you can use this:

XmlElement XmlEle = (XmlElement)xmlDoc.SelectSingleNode("dict/integer");

And now you can use the variable XmlEle to replace the InnerText because it's just a reference.

Like this:

XmlEle.InnerText = TopNumber.ToString();



回答4:


just use not innterxml , user text

 foreach (Paragraph paragraph in document.MainDocumentPart.Document.Descendants<DocumentFormat.OpenXml.Wordprocessing.Paragraph>())
            {
                bool ss = paragraph.InnerXml.Contains("commentRangeStart");
                bool ee = paragraph.InnerXml.Contains("commentRangeEnd");
                if (ss && ee)
                {
                    foreach (Run run in paragraph.Elements<Run>())
                    {
                        foreach (Text text in run.Elements<Text>())
                        {
                                text.Text = "your word " ;
                        }
                    }
                }
            }


来源:https://stackoverflow.com/questions/32075170/how-to-replace-the-innertext-of-a-comment

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