Frage

I'm trying to read a RSS feed from the web, and put it's result into an array. Unfortuneatly, I've been for 2 days on the web, searching for every solution, without success.

In the example at windows phone developers site :

private void loadFeedButton_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        WebClient webClient = new WebClient();
        webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
        webClient.DownloadStringAsync(new System.Uri("http://windowsteamblog.com/windows_phone/b/windowsphone/rss.aspx"));
    }

    // Event handler which runs after the feed is fully downloaded.
    private void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        if (e.Error != null)
        {
            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                MessageBox.Show(e.Error.Message);
            });
        }
        else
        {
            this.State["feed"] = e.Result;
            UpdateFeedList(e.Result);
        }
    }

    // This method sets up the feed and binds it to our ListBox. 
    private void UpdateFeedList(string feedXML)
    {
        StringReader stringReader = new StringReader(feedXML);
        XmlReader xmlReader = XmlReader.Create(stringReader);
        SyndicationFeed feed = SyndicationFeed.Load(xmlReader);

        Deployment.Current.Dispatcher.BeginInvoke(() =>
        {
            feedListBox.ItemsSource = feed.Items;
            loadFeedButton.Content = "Refresh Feed";
        });

    }

u can see that the "button_click" event ended, when it continues into the FinishDownload event and then it puts the result into the feedListBox Item Source.

Now, I don't want it. I wanna put the results into a list or any ADT like this

public static [ADT] Execute (string link)
{ .... }

so that this functions returns me the results from the feed.

Now I was trying anything, threading and XDocument, but it doesn't work.

War es hilfreich?

Lösung

So if I understand you correctly you want to download the feed and then just have the items in an List, array or similair instead of in the listbox? Then you can simpy add the line:

var feedItems = new List<SyndicationItem>(feed.Items);

I also get the feeling you want it all in one function instead, then you can do it this way using Tasks:

public static Task<List<SyndicationItem>> Execute(string link)
{
    WebClient wc = new WebClient();
    TaskCompletionSource<List<SyndicationItem>> tcs = new TaskCompletionSource<List<SyndicationItem>>();

    wc.DownloadStringCompleted += (s, e) =>
    {
        if (e.Error == null)
        {
            StringReader stringReader = new StringReader(e.Result);
            XmlReader xmlReader = XmlReader.Create(stringReader);
            SyndicationFeed feed = SyndicationFeed.Load(xmlReader);

            tcs.SetResult(new List<SyndicationItem>(feed.Items));
        }
        else
        {
            tcs.SetResult(new List<SyndicationItem>());
        }
    };
    wc.DownloadStringAsync(new Uri(link, UriKind.Absolute));
    return tcs.Task;
}

And then your event handler for the button would be:

private async void loadFeedButton_Click(object sender, System.Windows.RoutedEventArgs e)
{
    var rssItems = await Execute("http://windowsteamblog.com/windows_phone/b/windowsphone/rss.aspx");
    MessageBox.Show("Number of RSS items: " + rssItems.Count);
}

Note the async and await keywords, you can read more about those in the documentation.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top