[WP8.0]图像的HttpClient异常处理。 [英] [WP8.0] HttpClient Exception handling for image.
问题描述
在Windows Phone 8.0 Silverlight应用程序中使用HttpClient和HtmlAgility包。
Using HttpClient with HtmlAgility pack in a Windows Phone 8.0 Silverlight app.
我想知道如何管理图像未包含在特殊节点。
I'd like to know how to manage an exception where an image isn't contained in a particular node.
例如,缺少TEXT,我会使用以下内容:
For example, with missing TEXT, I would use the 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();
但是,如何为缺少的img src编写代码? 我的默认代码,无异常处理是:
But, how do I code this for a missing img src? My default code, without exception handling is:
newGame.Cover = div.SelectSingleNode(".//img[@class= 'box1']").Attributes["src"].Value;
理想情况下,如果节点中没有包含我自己的图片文件,我想使用我自己的图像文件(例如"/ Assets" /Images/Unavailable.png")
Ideally, I would like to use my own image file if one is not contained within the node (e.g. "/Assets/Images/Unavailable.png")
提前致谢。
Thanks in advance.
基思
推荐答案
你不能用这个吗?:
Can't you use this?:
XmlAttribute srcAttribute = (XmlAttribute)div.SelectSingleNode(".//img[@class='box1']").Attributes["src"];
if (srcAttribute != null)
{
newGame.Cover = srcAttribute.Value;
}
else
{
newGame.Cover = "/Assets/Images/Unavailable.png";
}
这篇关于[WP8.0]图像的HttpClient异常处理。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!