2014-04-05 92 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