读取RSS项目中的内部属性/标签,如windows phone 8中的(media:content)

时间:2014-02-28 15:59:19

标签: c# xml windows-phone-8 rss

我最近在Windows Phone 8应用程序中遇到了xml解析问题,我可以立即读取XML提要中的所有标签,如标题,描述等。但是我无法从包含“:”符号的标签中获取内部属性,例如:<media:content ... /><media:text ../>等。

以下是来自Yahoo的XML Feed:http://cricket.yahoo.com/news/rss/

每件商品都是这样的:

   <item>
      <title>Asia Cup: India vs Sri Lanka</title>
   <description>
       <p><a href="http://cricket.yahoo.com/prematch-india-vs-sri-lanka_188453"><img                     src="http://l3.yimg.com/bt/api/res/1.2/2rY73ROpgTT4wBxyQqSjwA--/YXBwaWQ9eW5ld3M7Zmk9ZmlsbDtoPTg2O3B4b2ZmPTUwO3B5b2ZmPTA7cT03NTt3PTEzMA--/http://media.zenfs.com/en_us/News/ap_webfeeds/112af677510d1a084d0f6a706700f2a0.jpg" width="130" height="86" alt="India’s Shikhar Dhawan plays a shot during the Asia Cup one-day international cricket tournament against Sri Lanka in Fatullah, near Dhaka, Bangladesh, Friday, Feb. 28, 2014. (AP Photo/A.M. Ahad)" align="left" title="India’s Shikhar Dhawan plays a shot during the Asia Cup one-day international cricket tournament against Sri Lanka in Fatullah, near Dhaka, Bangladesh, Friday, Feb. 28, 2014. (AP Photo/A.M. Ahad)" border="0" /></a>Fatullah - India lose quick wicket after Dhawan&#039;s dismissal for 94.</p><br clear="all"/>
    </description>
    <link>
       http://cricket.yahoo.com/prematch-india-vs-sri-lanka_188453
    </link>
   <pubDate>Fri, 28 Feb 2014 13:08:29 +0530</pubDate>
   <guid isPermaLink="false">prematch-india-vs-sri-lanka_188453</guid>
   <media:content url="http://l3.yimg.com/bt/api/res/1.2/2rY73ROpgTT4wBxyQqSjwA--/YXBwaWQ9eW5ld3M7Zmk9ZmlsbDtoPTg2O3B4b2ZmPTUwO3B5b2ZmPTA7cT03NTt3PTEzMA--/http://media.zenfs.com/en_us/News/ap_webfeeds/112af677510d1a084d0f6a706700f2a0.jpg" type="image/jpeg" width="130" height="86"/>
    <media:text type="html">
      <p><a href="http://cricket.yahoo.com/prematch-india-vs-sri-lanka_188453"><img src="http://l3.yimg.com/bt/api/res/1.2/2rY73ROpgTT4wBxyQqSjwA--/YXBwaWQ9eW5ld3M7Zmk9ZmlsbDtoPTg2O3B4b2ZmPTUwO3B5b2ZmPTA7cT03NTt3PTEzMA--/http://media.zenfs.com/en_us/News/ap_webfeeds/112af677510d1a084d0f6a706700f2a0.jpg" width="130" height="86" alt="India’s Shikhar Dhawan plays a shot during the Asia Cup one-day international cricket tournament against Sri Lanka in Fatullah, near Dhaka, Bangladesh, Friday, Feb. 28, 2014. (AP Photo/A.M. Ahad)" align="left" title="India’s Shikhar Dhawan plays a shot during the Asia Cup one-day international cricket tournament against Sri Lanka in Fatullah, near Dhaka, Bangladesh, Friday, Feb. 28, 2014. (AP Photo/A.M. Ahad)" border="0" /></a>Fatullah - India lose quick wicket after Dhawan&#039;s dismissal for 94.</p><br clear="all"/>
   </media:text>
    <media:credit role="publishing company"/>
  </item>

我的解析和获取数据的代码是:

try
{
    XNamespace media = "http://cricket.yahoo.com/news/rss/";

    XDocument xmlDoc = XDocument.Parse(e.Result);
    var all = xmlDoc.Descendants("channel");
    List<News1> _singleRsses = new List<News1>();
    foreach (var item in all)
    {
        var itemVedio = item.Descendants("item");
        foreach (var xElement in itemVedio)
        {
            string _title = xElement.Element("title").Value;
            string _description = xElement.Element("description").Value;
            string pubDate = DateTime.Parse(xElement.Element("pubDate").Value).ToString();
            string link = xElement.Element("link").Value;
            string _imageSource = xElement.Element(media+"content").Attribute("url").Value;

            News1 _singleRss = new News1
            {
                Title = _title,
                description = Regex.Replace(_description, "<.+?>", string.Empty),
                PubDate = pubDate,
                Link = link,
                Image=_imageSource
            };
            _singleRsses.Add(_singleRss);
        }
    }
    newsListbox.DataContext = _singleRsses;
}
catch (Exception exp)
{
    MessageBox.Show("Exception: " + exp);
    _progressIndicator.IsIndeterminate = false;
    _progressIndicator.IsVisible = false;
}


public class News1
{
    public string Title { get; set; }
    public string Image { get; set; }
    public string description { get; set; }
    public string PubDate { get; set; }
    public string Link { get; set; }
}

在一些教程之后我也使用了XNamespace,但它不起作用,我收到了这个错误:

Exception: system.nullReferenceException:  Object reference not set to an instance of an object

0 个答案:

没有答案