Receiving items from a feed - Xamarin

Asked

Viewed 30 times

0

With this method I search all the items of a url. Now I need to select only the fifth position item, how can I do this and what changes I should make?

private async Task<List<FeedItem>> ParseFeed(string rss)
    {
        return await Task.Run(() =>
        {
            var xdoc = XDocument.Parse(rss);
            var id = 0;
            return (from item in xdoc.Descendants("item")
                    let enclosure = item.Element("enclosure")
                    where enclosure != null
                    select new FeedItem
                    {
                        Title = (string)item.Element("title"),
                        Description = (string)item.Element("description"),
                        Link = (string)item.Element("link"),
                        PublishDate = DateTime.Parse((string)item.Element("pubDate")).ToUniversalTime().ToString("dd/MM/yyyy HH:mm:ss"),
                        Category = (string)item.Element("category"),
                        Mp3Url = (string)enclosure.Attribute("url"),
                        Image = (string)enclosure.Attribute("url"),
                        Color_category =Convert.ToString(int.Parse((string)item.Element("color")), 16).PadLeft(6, '0'),
                    Id = id++
                    }).ToList();
        });
    }

1 answer

0

Two things to observe here:

  1. His method is asynchronous (async) then for best result, you should expect the result of it through an operator called await.
  2. Your method returns a list of Feeditem then we should just store this return and fetch the position by the list index.

    // Armazena o feed em uma lista List<FeedItem> myFeedList = await ParseFeed("endereço do feed"); // Busca o quinto elemento da lista, neste caso representado pelo número 5 (coloque a posição que desejar) FeedItem feedItem = myFeedList[5];

Browser other questions tagged

You are not signed in. Login or sign up in order to post.