2013-04-09 92 views
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)); 

回答

0

您的XPath應該閱讀"//w:t='[ALL]']"