c# - HTML parsing with HtmlAgilityPack in Windows Store App -
i'm parsing html code image link in "windows store application" i'm using html agility pack ! here code :
async void loidcontent() { foreach (var feeditem in feeddata.items) { httpclient httpclient = new httpclient(); var stream = await httpclient.getstreamasync(feeditem.link); htmldocument htmldoc = new htmldocument(); htmldoc.load(stream); // image var div = htmldoc.documentnode.descendants().firstordefault( d => d.name == "div" && d.attributes["class"] != null && d.attributes["class"].value == "pika-stage img"); var img = div.descendants("img").firstordefault(); if (img != null) { string imglinks = img.attributes["src"].value; feeditem.image = new uri(imglinks); } }}
sometimes application crashes "object reference not set instance of object" in line
var img = div.descendants("img").firstordefault();
before this:
var img = div.descendants("img").firstordefault();
check null
:
if ((div != null) && (div.descendants("img") != null)) { // rest of code }
Comments
Post a Comment