1
我试图解析.docx文件的document.xml文件。我想搜索文本,然后返回文本所在的节点,以便我可以移动到父节点并插入新的节点类型。这是我到目前为止,我一直在尝试使用SelectSingle节点,但我似乎无法得到正确的路径(“路径是正确的直到/ w:身体)。所以我想只是搜索文本并得到节点这样,如果这就是可能的。获取Xml文本节点ID
NameTable nt = new NameTable();
XmlNamespaceManager nsManager = new XmlNamespaceManager(nt);
nsManager.AddNamespace("w", wordmlNamespace);
// Get the document part from the package.
// Load the XML in the document part into an XmlDocument instance.
XmlDocument xdoc = new XmlDocument(nt);
xdoc.Load(wdDoc.MainDocumentPart.GetStream());
XmlNode ALL = xdoc.SelectSingleNode("/w:document/w:body/w:p/w:r[w:t='[ALL]']", nsManager);
if (ALL != null)
{
XmlElement vanish = xdoc.CreateElement("//w:vanish /");
XmlNode topNode = ALL.ParentNode.ParentNode;
XmlNode topParentNode = topNode.ParentNode;
topParentNode.InsertBefore(vanish,topParentNode.FirstChild);
}
// Save the document XML back to its document part.
xdoc.Save(wdDoc.MainDocumentPart.GetStream(FileMode.Create, FileAccess.Write));