Search code examples
c#silverlightwindows-phone-7expression-blend

how to sort feed by category if im using syndication in Windows Phone 7?


Im writing an Rss reader. My question is: can I sort by category if im using syndicationfeed? the idea is for the feed to already be sorted when it is displayed in listbox. Sort and display on 1 predefined category only.

Im a new programer so Im working on this MSDN example: http://msdn.microsoft.com/en-us/library/hh487167(v=vs.92).aspx

RSS feed i'm using is: http://www.zimo.co/feed/


UPDATE:

my new try to solve but fail:

private void UpdateFeedList(string feedXML)
    {
        StringReader stringReader = new StringReader(feedXML);
        XmlReader xmlReader = XmlReader.Create(stringReader);
                                                              //problem below (I do not get sort method)
        SyndicationFeed feed = SyndicationFeed.Load(xmlReader).Categories.sort();


        Deployment.Current.Dispatcher.BeginInvoke(() =>
        {
           //binding listbox with feed items
            ls_text.ItemsSource = feed.Items;



        });
    }

Solution

  • UPDATE I dug deeper into this. First of all, it makes no sense to me to order items by categories just because every item has more than one category. As you indicated below, you were interested in filtering. Here is how I did that (the code goes into the UpdateFeedList event):

    //same code as in the example
    SyndicationFeed feed = SyndicationFeed.Load(xmlReader);
    
    //I pull out the distinct list of categories from the entire feed
    //You might want to bind this list to a dropdown on your app
    //so that users can select what they want to see:
    List<string> cats = new List<string>();
    foreach (SyndicationItem si in feed.Items)
        foreach(SyndicationCategory sc in si.Categories)
           if (!cats.Contains(sc.Name))
              cats.Add(sc.Name);
    
    //Here I imagined that I want to see all items categorized as "Trendy"
    SyndicationCategory lookingFor = new SyndicationCategory("Trendy");
    
    //and I create a container where I could copy all "Trendy" items:
    List<SyndicationItem> coll = new List<SyndicationItem>();
    
    //And then I start filtering all items by the selected tag:
    foreach (SyndicationItem si in feed.Items)
    {
         foreach (SyndicationCategory sc in si.Categories)
             if (sc.Name == lookingFor.Name)
             {
                        coll.Add(si);
                        break;
             }
    }
    
    Deployment.Current.Dispatcher.BeginInvoke(() =>
    {
        // Bind the filtered of SyndicationItems to our ListBox.
           feedListBox.ItemsSource = coll;
    
           loadFeedButton.Content = "Refresh Feed";
    });
    

    WHATEVER BELOW IS INCORRECT

    yoursyndicationfeed.Categories.Sort()
    

    I personally would not be sorting an RSS feed by anything except for the date descending (it should be sorted so already) because an RSS feed gives me the updates for the web site content in the reverse order of them being published and that I how most people are used to see it :)

    Another update (also incorrect :))

    Ok, so I still don't have the code for that app in front of me, but digging around documentation (that I mentioned in the comments), here is what you can do:

    SyndicationFeed feed = SyndicationFeed.Load(xmlReader); 
    feed.Items.OrderBy(x => x.Category);
    

    The idea is that feed.Items is a List of your elements. So treat it like a list, sort it by the property that you need.

    Oh, also, I saw your other question on the RSS reader, do you have to use the sample? Scott Guthrie wrote an RSS client for Windows Phone 7, his implementation seems a bit simpler. It depends on your needs, of course.