获取Xml文本节点ID

时间:2013-04-09 14:57:54

标签: c# xml xml-parsing docx xmlnode

我正在尝试解析.docx文件的document.xml文件。我想搜索Text然后返回文本所在的节点,这样我就可以向上移动到父节点并插入新的节点类型。这是我到目前为止,我一直在尝试使用SelectSingle节点,但我似乎无法正确的路径(“路径是正确的直到/ W:正文)。所以我想只搜索文本并在可能的情况下获取节点。

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

1 个答案:

答案 0 :(得分:0)

你的xpath应该是"//w:t='[ALL]']"