【问题标题】:How to recuperate the image of this rss feed ?如何恢复这个 rss 提要的图像?
【发布时间】:2012-03-08 23:42:40
【问题描述】:
我正在开发一个 wp7 应用程序,它是一个简单的 rss 阅读器。我可以恢复日期、标题和描述...
但是当我试图从这个rss feed 中恢复图像时,我发现了一个 NullReferenceException... 这里是错误的行:
itemRss.Image = new Uri(item.Element("enclosure").Attribute("url").Value);
那么,请问恢复图像的好方法是什么?提前致谢
【问题讨论】:
标签:
c#
windows-phone-7
silverlight-4.0
rss-reader
【解决方案1】:
此提要中没有“附件”元素。
当您说图像时,它是包含在文本中的图像吗?如果是这样,请使用“内容”元素检索 HTML 并使用 the regex that I have already given in this answer。
var reg = new Regex("src=(?:\"|\')?(?<imgSrc>[^>]*[^/].(?:jpg|bmp|gif|png))(?:\"|\')?");
var match=reg.Match(source);
if(match.Success)
{
var encod = match.Groups["imgSrc"].Value;
}
【解决方案2】:
你需要从<img src="http://www.artdeseduire.com/wp-content/uploads/2012/02/Comment-choisir-son-jean.jpg" alt="Comment choisir son jean Comment choisir son jean simplement et rapidement..." title="Comment choisir son jean" width="207" height="302" class="alignright size-full wp-image-14072" />恢复uri;
var reg1 = new Regex("src=(?:\"|\')?(?<imgSrc>[^>]*[^/].(?:jpg|bmp|gif|png))(?:\"|\')?");
var match1 = reg1.Match(source);
if (match1.Success)
{
temp.UrlImage = new Uri(match1.Groups["imgSrc"].Value, UriKind.Absolute);
}