我汇总了很多RSS源,有些源Feed有媒体标签,但很多来源都没有。 如何跳过图像
Image =((string)x.Element(“enclosure”)。属性(“url”)),
在控制器中,如果标记不存在以进行记录,因为如果不存在,则标记我的网站为什么?
控制器代码:
public ActionResult Index()
{
WebClient wclient = new WebClient(/*string RSSURL*/);
wclient.Encoding = Encoding.UTF8;
var RSSURL = "http://localhost:60348/Rss";
string RSSData = wclient.DownloadString(RSSURL);
XDocument xml = XDocument.Parse(RSSData);
var RSSFeedData = (from x in xml.Descendants("item")
select new RSSFeed
{
Title = ((string)x.Element("title")),
Link = ((string)x.Element("link")),
Description = ((string)x.Element("description")),
PubDate = ((string)x.Element("pubDate")),
Image = ((string)x.Element("enclosure").Attribute("url")),
Kategorija = ((string)x.Element("category"))
}
);
ViewBag.RssFeed = RSSFeedData;
ViewBag.URL = RSSURL;
return View();
}
答案 0 :(得分:0)
您可以在VS 2015和VS 2017中使用null-conditional operator(?。)Image = ((string)x.Element("enclosure")?.Attribute("url"))
或旧版VS Image = x.Element("enclosure") != null ? ((string)x.Element("enclosure").Attribute("url")) : null