2

我无法想出正确的代码来从 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)
        };
}
4

1 回答 1

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)
        };
}
于 2013-05-06T06:24:03.000 回答