2014-04-05 370 views
1

你好,我有一些問題,當我試圖讓子屬性 例的Html敏捷包選擇子屬性

<p attribute:subattribue="mytext">hello world<p> 

我試着這樣做:

textblock1.Text = doc.DocumentNode.SelectSingleNode("//p[@attribute:subattribute='mytext']").InnerText.Trim(); 

很抱歉,但我是一個新手

回答

1

一般來說,在XPath中,您可以使用以下表達式來獲取屬性名稱等於attribute:subattribute<p>元素以及屬性值等於mytext

//p[@*[name()='attribute:subattribute' and .='mytext']] 

不幸的是,上面的XPath不使用HtmlAgilityPack工作(返回null當我嘗試)。但有一個解決方法,通過使用LINQ來查詢具有上述相同條件的數據XPath:

HtmlNode n = doc.DocumentNode 
       .SelectNodes("//p") 
       .Where(o => o.Attributes["attribute:subattribute"] != null && 
          o.Attributes["attribute:subattribute"].Value == "myValue") 
       .FirstOrDefault(); 
textblock1.Text = n.InnerText.Trim(); 
+0

謝謝!有用 – Quakenxt