2014-10-18 50 views
5

我有一個像下面的HTML頁面。我需要從'span'標記中單獨使用'blah blah blah'。如何在沒有使用HtmlAgilityPack的子標籤的情況下單獨獲取內網文本?

<span class="news"> 
blah blah blah 
<div>hello</div> 
<div>bye</div> 
</span> 

這給了我所有的值:

div.SelectSingleNode(".//span[@class='news']").InnerText.Trim(); 

這讓我空:

div.SelectSingleNode(".//span[@class='news']/preceding-sibling::text()").InnerText.Trim(); 

我如何使用HtmlAgilityPack的 '格' 標籤之前的文本?

回答

8

你的第二次嘗試是非常接近。使用/text()代替/preceding-sibling::text(),因爲文本節點是孩子span[@class='news']兄弟(既不之前也不以下):

div.SelectSingleNode(".//span[@class='news']/text()") 
    .InnerText 
    .Trim(); 
+0

一塊巨大的答案,並感謝您指出我的錯誤! – 2014-10-18 10:52:16

相關問題