Nell'ultima versione del .NET Framework 3.5 vengono fornite, all'interno del namespace System.ServiceModel.Syndication, delle classi apposite per la lettura e scrittura dei feed nel formato RSS oppure ATOM.

Se, per esempio, volessimo offrire nella nostra web application un servizio di feed con le ultime novità, non dovremo più utilizzare classi esterne o dedicarci alla scrittura di una funzione apposita, ma utilizzare direttamente queste classi.

Vediamo un semplice esempio di codice per fare ritornare ad una nostra pagina ASP.NET l'output in formato RSS/ATOM di un ipotetico nostro blog (è necessario referenziare anche la dll "System.ServiceModel.Web" nel progetto):

using System;
using System.Xml;
using System.Collections.Generic;
using System.ServiceModel.Syndication;

namespace RssGenerator
{
  public partial class _Default : System.Web.UI.Page
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      // Contenuto ipotetico del blog
      var feeds=new []{
        new {Id = 1, Title="Titolo 1", Body="Testo del primo rss", Date=new DateTime(2008,1,1), Category=".Net"},
        new {Id = 2, Title="Titolo 2", Body="Testo del secondo rss", Date=new DateTime(2008,1,2), Category="C#"},
        new {Id = 3, Title="Titolo 3", Body="Testo del terzo rss", Date=new DateTime(2008,1,3), Category=".Net"},
        new {Id = 4, Title="Titolo 4", Body="Testo del quarto rss", Date=new DateTime(2008,1,4), Category=".Net"},
        };

      // Categorie
      var categories = new[] {
        ".Net", "C#" };

      using (var writer = XmlWriter.Create(Response.OutputStream))
      {
        // Creo il feed impostanto le proprietà di base
        var feed = new SyndicationFeed("AZ", "Testo descrittivo rss", new Uri("http://AZ.AZ"));

        // Aggiungo l'autore
        feed.Authors.Add(new SyndicationPerson("andrew@aspitalia.com", "AZ",  "http://ciclismo.sitiasp.it"));

        // Aggiungo categorie al feed
        foreach (var category in categories) feed.Categories.Add(new SyndicationCategory(category));
        
        // Copyright
        feed.Copyright = new TextSyndicationContent("© AZ for AspItalia.com");

        // chi ha generato
        feed.Generator = "AZ";

        // Culture utilizzata
        feed.Language = "it-IT";

        // Aggiungo ogni feed alla collection
        List<SyndicationItem> items = new List<SyndicationItem>();
        foreach (var singleFeed in feeds)
        {
          var item = new SyndicationItem();
          item.Id = singleFeed.Id.ToString();
          item.Title = TextSyndicationContent.CreatePlaintextContent(singleFeed.Title);
          item.Content = SyndicationContent.CreateXhtmlContent(singleFeed.Body);
          item.PublishDate = singleFeed.Date;
          item.Categories.Add(new SyndicationCategory(singleFeed.Category));
          // Link di ogni singolo post 
          item.AddPermalink(new Uri("http://www.sito.xx/pagina.aspx?id=" + singleFeed.Id));
          items.Add(item);
        }

        feed.Items = items;

        Response.Clear();
        Response.ContentType = "application/xml";

        // Scrive feed!

        // Con Atom, decommentare questo codice per utilizzare ATOM
        //var atomFormatter = new Atom10FeedFormatter(feed);
        //atomFormatter.WriteTo(writer);

        // Con Rss 2, commentare questo codice per usare ATOM
        var rssFormatter = new Rss20FeedFormatter(feed);
        rssFormatter.WriteTo(writer);
      }
      Response.End();
    }
  }
}

Per leggere RSS e Atom, si veda:
#943 - Leggere feed RSS e Atom con il .NET Framework 3.5
https://www.aspitalia.com/script/943/Leggere-Feed-RSS-Atom-.NET-Framework-3.5.aspx

Commenti

Visualizza/aggiungi commenti

| Condividi su: Twitter, Facebook, LinkedIn

Per inserire un commento, devi avere un account.

Fai il login e torna a questa pagina, oppure registrati alla nostra community.

Approfondimenti

I più letti di oggi