简体   繁体   English

在Windows Store应用中使用HtmlAgility包

[英]using HtmlAgility pack in a Windows Store app

In my Windows Store app, I've got the following piece of code to extract a div with the id=pos_0 在我的Windows应用商店应用中,我获得了以下代码来提取id = pos_0的div。

HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(my_html_string);

var div_i_need = doc.DocumentNode.Descendants("div").Where(x => x.Attributes.Contains("id") && x.Attributes["id"].Value == "pos_0").FirstOrDefault();

Is there an easier way to do this? 有没有更简单的方法可以做到这一点? SelectSingleNode doesn't exist in this version of the library, and Descendants doesn't seem to accept XPath 此版本的库中不存在SelectSingleNode ,并且Descendants似乎不接受XPath

HtmlAgilityPack can't perform XPath query since .NET doesn't have XPath implementation for Windows Store. HtmlAgilityPack无法执行XPath查询,因为.NET没有Windows Store的XPath实现。 Your code can be simplified a bit by using GetAttributeValue() method and strip out Where() : 使用GetAttributeValue()方法可以简化代码,并GetAttributeValue() Where()

var div_i_need = doc.DocumentNode
                    .Descendants("div")
                    .FirstOrDefault(x => x.GetAttributeValue("id", "") == "pos_0");

GetAttributeValue() returns the second parameter if the attribute is not found 如果找不到属性,则GetAttributeValue()返回第二个参数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM