HtmlAgilityPack-PCL + LINQ

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-11 07:13:35

问题


Well, basically I have a Windows Phone 8.1 app that's supposed to download the html file and parse it using HtmlAgilityPack-PCL and LINQ.

var nodes = from tr in doc.DocumentNode.Descendants("body")
                from td in tr.Descendants("div").Where(x =>
x.Attributes["id"].Value == "screen")select tr;

Then I'm trying to get the node from nodes:

        HtmlNode node = nodes.FirstOrDefault();

And this is the point where i have an exeption "Object reference not set to an instance of an object." The html file definitely has the div I am looking for. So what am I doing wrong?


回答1:


you probably mean

var node = doc.DocumentNode.Descendants("div").
               Where(div => div.GetAttributeValue("id", string.Empty) == "screen").
               FirstOrDefault();

if that does not work feel free to share your html or the relevant part of it.



来源:https://stackoverflow.com/questions/29689499/htmlagilitypack-pcl-linq

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!