2013-08-05 82 views
0

我有以下的HTMLXPATH:如何让孩子节点

 <ul class="enh-toggle"> 
     <li> 
     <a href="#design">Design<sup>1</sup><span class="accordion"></span></a> 
     <ul id="design"> 
       <li> 
        <strong>Dimensions</strong> 
        <ul><li>length:12.3cm</li></ul> 
       </li> 
      </ul> 
      </li> 
     </ul> 

我使用下面的代码来获得UL [ID = '设计']

HTMLNode node = doc.DocumentNode.SelectSingleNode("//ul[@class='enh-toggle']//ul[@id='design']"); 

这只是工作完美...

现在我的问题是如何获得强标记文本。我用下面的代码,但它不工作

string text = node.SelectSingleNode("/li/strong").InnerText; 

回答

2

变化:

string text = node.SelectSingleNode("./li/strong").InnerText; 
+1

我喜欢这个版本,因为它明确地表示并从当前情况下,而不是根源。 –

1

XPath中一个单斜杠是文档的根。你只是想选择直接后代,所以你并不需要给出一个情境:

string text = node.SelectSingleNode("li/strong").InnerText; 
1

我认为它应该仅仅是:

string text = node.SelectSingleNode("li/strong").InnerText; 

..无领导/。在“丽/强”的回答