using httpclient htmlagility pack in windows phone 8.0 silverlight app.
i'd know how manage exception image isn't contained in particular node.
for example, missing text, use following:
var notitle = div.selectsinglenode(".//h3"); if (notitle == null || string.isnullorempty((notitle.innertext ?? "").trim())) newgame.title = "unavailable"; else newgame.title = div.selectsinglenode(".//h3").innertext.trim();
but, how code missing img src? default code, without exception handling is:
newgame.cover = div.selectsinglenode(".//img[@class= 'box1']").attributes["src"].value;
ideally, use own image file if 1 not contained within node (e.g. "/assets/images/unavailable.png")
thanks in advance.
31/01/2016 - added html code
this snippet of html i'm getting info from.
<div class="game-c"> <div class="boxshot"> <img class="cover" src="http://cover_source" /> </div> <h3 class="h3 white-c">game title ...</h3> <p>game description goes here...</p> <p>...</p> <div class="cta-signedout">...</div> <div class="cta-signedin"> <a href="https://link.to.store"> <img src="gameimage.gif" /></a> </div> </div>
i need img src 'cta-signedin'.
htmlagilitypack has getattributevalue()
method suitable requirement. 2nd parameter of method specify default value returned when attribute not found :
newgame.cover = div.selectsinglenode(".//img[@class= 'box1']") .getattributevalue("src", "/assets/images/unavailable.png");
the above snippet assumed <img>
element exists, it's src
attribute missing. otherwise, you'll need check whether or not selectsinglenode()
returns null
first.
here simple working demo example :
var html = @"<div><img/></div>"; htmldocument doc = new htmldocument(); doc.loadhtml(html); var src = doc.documentnode .selectsinglenode("//img") .getattributevalue("src", "/assets/images/unavailable.png"); console.writeline(src);
output :
/assets/images/unavailable.png
Comments
Post a Comment