Feb
06

Ler um RSS sem o uso do LINQ é preciso usar algumas estruturas de repetições, que pode até deixar o algoritmo um pouco confuso. A algum tempo atrás eu fiz um leitor de RSS em PHP (está hospedado no PHP Brasil), ficou em um ótimo algoritmo, mas deve ter dado 3x mais linha do que agora com o LINQ. 

Sempre me perguntei "Como será uma linguagem sem usar as comuns estruturas de condições e repetições?", mas depois que li um RSS usando LINQ, eu percebi minha pergunta já estava sendo respondida. Acredito que o LINQ será o PAI das novas linguagens.

Mas chega de tanto blablabla, abaixo está o exemplo:

 

Em C#:

   1: protected void Page_Load(object sender, EventArgs e)
   2: {
   3:     if (!IsPostBack)
   4:         CarregaRss(@"http://feeds.feedburner.com/magodigital");
   5: }
   6:  
   7: protected void CarregaRss(string url)
   8: {
   9:     XDocument feedDoc = XDocument.Load(url);
  10:  
  11:     var query = from feed in feedDoc.Descendants("channel")
  12:                 select new
  13:                 {
  14:                     Titulo = feed.Element("title").Value,
  15:                     Descricao = feed.Element("description").Value,
  16:                     Link = feed.Element("link").Value,
  17:                     Itens = (from feedItens in feed.Descendants("item")
  18:                             select new
  19:                             {
  20:                                 Titulo = feedItens.Element("title").Value,
  21:                                 Link = feedItens.Element("link").Value,
  22:                                 Descricao = feedItens.Element("description").Value
  23:                             }).ToList()
  24:  
  25:                 };
  26:  
  27:     var rss = query.Single();
  28:  
  29:     LblTitulo.Text = rss.Titulo;
  30:     LblDescricao.Text = rss.Descricao; 
  31:     HypLink.Text = HypLink.NavigateUrl = rss.Link;
  32:     GridItems.DataSource = rss.Itens;
  33:     GridItems.DataBind();
  34: }

 

Em Asp.NET:

   1: <form id="form1" runat="server">
   2:     Titulo :  <asp:Label ID="LblTitulo" runat="server" /><br />
   3:     Descricao: <asp:Label ID="LblDescricao" runat="server" /><br />
   4:     Link:  <asp:HyperLink ID="HypLink" runat="server" /><br />
   5:     <asp:GridView ID="GridItems" runat="server" />
   6: </form>

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

.NET | ASP.NET | LINQ

Tags: ,

Add comment


 

  Country flag

biuquote
Loading



Search


Novidades no seu e-mail


Recent posts

Recent comments

Designer Original Binary Bonsai | Adaptado para MagoDigital | BlogEngine.NET