无法从RSS源下载图片?

时间:2013-04-30 03:31:19

标签: c# windows-phone

我无法想出正确的代码来从RSS源下载图片,然后将该下载文件交给RadControls幻灯片查看或分页。

我使用的代码唯一可以得到的是图片的文字或图片的缩略图,而不是完整的图片。我必须遗漏某些东西或遗漏一些东西。

这适用于Windows Phone C#

RSS源的Web链接是通用的,用于测试目的。

//Constructor
public MainPage()
{
    InitializeComponent();
}

private void FlickrSearch_Click(object sender, RoutedEventArgs e)
{
    WebClient webclient = new WebClient();
    webclient.DownloadStringCompleted += new DownloadStringCompletedEventHandler         

    (webclient_DownloadStringCompleted);             
}

void webclient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
    if (e.Error != null)
    {
        MessageBox.Show("error");
    }
    // parsing Flickr 

    XElement XmlTweet = XElement.Parse(e.Result);
    XNamespace ns = "http://api.flickr.com/services/feeds/photos_public.gne?tag="; // flilckr
    listBox1.ItemsSource = 
        from tweet in XmlTweet.Descendants("item")
        select new FlickrData
        {
            ImageSource = tweet.Element(ns + "thumbnail").Attribute("url").Value,
            Message = tweet.Element("description").Value,
            UserName = tweet.Element("title").Value,
            PubDate = DateTime.Parse(tweet.Element("pubDate").Value)
        };
}

1 个答案:

答案 0 :(得分:2)

我找到了你解析的方式&获取内部数据是错误的。我已经仔细验证了Flickr的XML数据并相应地修改了逻辑。

以下是完整的代码:

void webclient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
    if (e.Error != null)
    {
        MessageBox.Show("error");
    }
    // parsing Flickr 

    XElement XmlTweet = XElement.Parse(e.Result);
    string ns = "http://www.w3.org/2005/Atom";

    XName entry = XName.Get("entry", ns);
    XName loc = XName.Get("loc", ns);
    XName title = XName.Get("title", ns);
    XName published = XName.Get("published", ns);
    XName link = XName.Get("link", ns);
    XName content = XName.Get("content", ns);
    XName url = XName.Get("url", ns);

    listBox1.ItemsSource = 
        from tweet in XmlTweet.Elements(entry)
        select new FlickrData
        {
            ImageSource = tweet.Element(link).Attribute("href").Value,
            Message = tweet.Element(content).Value,
            UserName = tweet.Element(title).Value,
            PubDate = DateTime.Parse(tweet.Element(published).Value)
        };
}