Hi,
I am trying to add a comment in a docx file programmatically through C# based on the condition if the text encountered is enclosed in square bracketts but it caters only first encounter of the square brackets case. It throws an exception when it encounters the second instance of square brackett.
The exception says OpenXMLPackage Exception was unhandled "Only one instance of the type is allowed for this parent."
Please find the code below and help me in resolving it.Thanks in advance
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DocumentFormat.OpenXml.Packaging;
using System.Xml;
using DocumentFormat.OpenXml.Drawing;
namespace CommentsOOXML
{
public partial class Comments : Form
{
XmlNamespaceManager nsManager = null;
public Comments()
{
InitializeComponent();
}
private void Comments_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
const string wordmlNamespace = "http://schemas.openxmlformats.org/wordprocessingml/2006/main";
WordprocessingDocument wordDoc = WordprocessingDocument.Open(@"c:\test3.docx", true);
MainDocumentPart docPart = wordDoc.MainDocumentPart;
XmlDocument xdoc = new XmlDocument();
xdoc.Load(docPart.GetStream());
XmlElement commentStartnode = xdoc.CreateElement("w:commentRangeStart", wordmlNamespace);
XmlAttribute attr = commentStartnode.Attributes.Append(xdoc.CreateAttribute("w:id", wordmlNamespace));
attr.Value = "0";
XmlNode commentNode = null;
NameTable nt = new NameTable();
nsManager = new XmlNamespaceManager(nt);
nsManager.AddNamespace("w", wordmlNamespace);
string charremove="[ ]";
int i = 0;
XmlNodeList comm = xdoc.SelectNodes("//w:r", nsManager);
foreach (XmlNode nod in comm)
{
string commentNote = nod.InnerText.ToString().Trim();
if (commentNote.Contains("["))
{
commentNode = nod;
XmlNode node1 = commentNode.ParentNode;
node1.InsertBefore(commentStartnode, commentNode);
XmlElement commentEndnode = xdoc.CreateElement("w:commentRangeEnd", wordmlNamespace);
XmlAttribute attr1 = commentEndnode.Attributes.Append(xdoc.CreateAttribute("w:id", wordmlNamespace));
attr1.Value = "0";
node1.InsertAfter(commentEndnode, commentNode);
XmlElement commentRefnode = xdoc.CreateElement("w:r", wordmlNamespace);
XmlNode node2 = commentNode.ParentNode;
node2.AppendChild(commentRefnode);
XmlElement commentRefnode1 = xdoc.CreateElement("w:commentReference", wordmlNamespace);
XmlAttribute attr2 = commentRefnode1.Attributes.Append(xdoc.CreateAttribute("w:id", wordmlNamespace));
attr2.Value = "0";
commentRefnode.AppendChild(commentRefnode1);
xdoc.Save(docPart.GetStream(FileMode.Create, FileAccess.Write));
XmlDocument commt = new XmlDocument();
WordprocessingCommentsPart commentpart = docPart.AddNewPart<WordprocessingCommentsPart>();
commt.Load(@"..\..\comments.xml");
XmlNode comment = commt.SelectSingleNode("//w:r", nsManager);
XmlElement cmtText = commt.CreateElement("w:t", wordmlNamespace);
comment.AppendChild(cmtText);
XmlNode node10 = comm.Item(i);
XmlNode commentText = commt.CreateNode(XmlNodeType.Text, "w:t", wordmlNamespace);
commentText.Value = node10.InnerText.Trim(charremove.ToCharArray());
cmtText.AppendChild(commentText);
StreamWriter commentpartWrt = new StreamWriter(commentpart.GetStream(FileMode.Create, FileAccess.Write));
commt.Save(commentpartWrt);
}
i++;
}
wordDoc.Close();
MessageBox.Show("done");
}
private void button2_Click(object sender, EventArgs e)
{
Close();
}
}
}