[英]Html Agility Pack selecting subattributes
您好,当我尝试获取子属性时遇到一些问题
<p attribute:subattribue="mytext">hello world<p>
我试图这样做:
textblock1.Text = doc.DocumentNode.SelectSingleNode("//p[@attribute:subattribute='mytext']").InnerText.Trim();
对不起,我是新手
一般来说,在XPath中可以使用下面的表达式来获得<p>
元件具有属性名称等于attribute:subattribute
和属性值等于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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.