1

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

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

1 回答 1

0

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

于 2013-04-15T18:48:46.813 回答