Cliente RSS con .NET LINQ

Veremos un pequeño cliente RSS, bajo la forma de aplicación de consola, que utiliza LINQ de .NET para parsear el archivo xml de una forma elegante y concisa.

Primero, definimos las clases Channel e Item, que darán forma a los objetos de un documento RSS:

class Channel
    {
        public string               Title       { get; set; }
        public string               Link        { get; set; }
        public string               Description { get; set; }
        public IEnumerable<Item>    Items       { get; set; }
    }

    class Item
    {
        public string Title         { get; set; }
        public string Link          { get; set; }
        public string Description   { get; set; }
        public string Guid          { get; set; }
    }

Luego creamos el método que obtiene (utilizando LINQ) el listado de Items de los Channels del feed:

static IEnumerable<Channel> getChannelQuery(XDocument xdoc)
        {
            return from channels in xdoc.Descendants("channel")
                   select new Channel
                   {
                       Title = channels.Element("title") != null ? channels.Element("title").Value : "",
                       Link = channels.Element("link") != null ? channels.Element("link").Value : "",
                       Description = channels.Element("description") != null ? channels.Element("description").Value : "",
                       Items = from items in channels.Descendants("item")
                               select new Item
                               {
                                   Title = items.Element("title") != null ? items.Element("title").Value : "",
                                   Link = items.Element("link") != null ? items.Element("link").Value : "",
                                   Description = items.Element("description") != null ? items.Element("description").Value : "",
                                   Guid = (items.Element("guid") != null ? items.Element("guid").Value : "")
                               }
                   };
        }

Finalmente, le solicitamos al usuario el path del feed a consultar y presentamos las entradas por consola:

static void Main()
        {
            Console.WriteLine("Cliente RSS con .NET LINQ");
            Console.Write("Ingrese URL de feed RSS:");
            String feedUri = Console.In.ReadLine();

            var myFeed = getChannelQuery(XDocument.Load(new StreamReader(HttpWebRequest.Create(feedUri).GetResponse().GetResponseStream())));
            foreach (var item in myFeed)
            {
                Console.WriteLine("{0} - {1}", item.Title, item.Description);

                foreach (var i in item.Items)
                {
                        Console.WriteLine("{0}", i.Title);
                }
            }
            Console.WriteLine("Presione cualquier tecla para continuar...");
            Console.ReadKey();
        }

Vía: Coding Day

Deje su comentario

Por favor, ingrese su nombre

Por favor, ingrese un correo-e válido

Por favor, ingrese su mensaje

luauf.com 2012

WordPress