Cómo leer o consumir Rss Feed en .Net

En éste artículo vamos a explicar cómo se puede crear una aplicación genérica para leer o consumir RSS de cualquier sitio web. Ojo que no se explica como darle formato o un estilo css para dejarlo vistoso, ya va a depender de su creatividad para dejarlo con más estilo.

Abra un proyecto. Cree una página RSSFeedReader.aspx. Agregue un control table del lado del servidor. Vamos a agregar filas con el contenido del RSS en ésta tabla. El código HTML quedaría así:

<table runat="server" id="tbl_Feed_Reader" cellpadding="0" cellspacing="0">
</table>

Ahora escribamos el código siguiente en el evento Page_Load:

C#:

protected void Page_Load(object sender, EventArgs e)
{
    //Cargar el sitio RSS feed, se usó la página del autor para el ejemplo
    WebRequest MyRssRequest = WebRequest.Create("http://codedisplay.com/?feed=rss");
    WebResponse MyRssResponse = MyRssRequest.GetResponse();
 
    Stream MyRssStream = MyRssResponse.GetResponseStream();
 
    XmlDocument MyRssDocument = new XmlDocument();
    MyRssDocument.Load(MyRssStream);
 
    XmlNodeList MyRssList = MyRssDocument.SelectNodes("rss/channel/item");
 
    string sTitle = "";
    string sLink = "";
    string sDescription = "";
 
    //Recorremos todos los items del RSS Feed
    for (int i = 0; i < MyRssList.Count; i++)
    {
        XmlNode MyRssDetail;
 
        MyRssDetail = MyRssList.Item(i).SelectSingleNode("title");
        if (MyRssDetail != null)
            sTitle = MyRssDetail.InnerText;
        else
            sTitle = "";
 
        MyRssDetail = MyRssList.Item(i).SelectSingleNode("link");
        if (MyRssDetail != null)
            sLink = MyRssDetail.InnerText;
        else
            sLink = "";
 
        MyRssDetail = MyRssList.Item(i).SelectSingleNode("description");
        if (MyRssDetail != null)
            sDescription = MyRssDetail.InnerText;
        else
        {
            sDescription = "";
        }
 
        // Acá se genera el códgio HTML par la tabla, filas, celdas basado en Título,Link y Descripción
        HtmlTableCell block = new HtmlTableCell();
         
        // En ésta parte se puede estilizar al gusto
        block.InnerHtml = "<span style='font-weight:bold'><a href='" + sLink + "' target='new'>" + sTitle + "</a></span>";
        HtmlTableRow row = new HtmlTableRow();
        row.Cells.Add(block);
        tbl_Feed_Reader.Rows.Add(row);
        HtmlTableCell block_description = new HtmlTableCell();
         
        // Estilizando la descripción
        block_description.InnerHtml = "<p align='justify'>" + sDescription + "</p>";
        HtmlTableRow row2 = new HtmlTableRow();
        row2.Cells.Add(block_description);
        tbl_Feed_Reader.Rows.Add(row2);
    }
}

Vb.Net:

Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    'Cargar el sitio RSS feed, se usó la página del autor para el ejemplo
    Dim MyRssRequest As WebRequest = WebRequest.Create("http://codedisplay.com/?feed=rss")
    Dim MyRssResponse As WebResponse = MyRssRequest.GetResponse()
 
    Dim MyRssStream As Stream = MyRssResponse.GetResponseStream()
 
    Dim MyRssDocument As XmlDocument = New XmlDocument()
    MyRssDocument.Load(MyRssStream)
 
    Dim MyRssList As XmlNodeList = MyRssDocument.SelectNodes("rss/channel/item")
 
    Dim sTitle As String = ""
    Dim sLink As String = ""
    Dim sDescription As String = ""
 
    'Recorremos todos los items del RSS Feed
    For i As Integer = 0 To MyRssList.Count - 1
        Dim MyRssDetail As XmlNode
 
        MyRssDetail = MyRssList.Item(i).SelectSingleNode("title")
        If Not MyRssDetail Is Nothing Then
            sTitle = MyRssDetail.InnerText
        Else
            sTitle = ""
        End If
 
        MyRssDetail = MyRssList.Item(i).SelectSingleNode("link")
        If Not MyRssDetail Is Nothing Then
            sLink = MyRssDetail.InnerText
        Else
            sLink = ""
        End If
 
        MyRssDetail = MyRssList.Item(i).SelectSingleNode("description")
        If Not MyRssDetail Is Nothing Then
            sDescription = MyRssDetail.InnerText
        Else
            sDescription = ""
        End If
 
        'Acá se genera el códgio HTML par la tabla, filas, celdas basado en Título,Link y Descripción
        Dim block As HtmlTableCell = New HtmlTableCell()
 
        'En ésta parte se puede estilizar al gusto
        block.InnerHtml = "<span style='font-weight:bold'><a href='" + sLink + "' target='new'>" + sTitle + "</a></span>"
        Dim row As HtmlTableRow = New HtmlTableRow()
        row.Cells.Add(block)
        tbl_Feed_Reader.Rows.Add(row)
        Dim block_description As HtmlTableCell = New HtmlTableCell()
 
        'Estizando la descripción
        block_description.InnerHtml = "<p align='justify'>" + sDescription + "</p>"
        Dim row2 As HtmlTableRow = New HtmlTableRow()
        row2.Cells.Add(block_description)
        tbl_Feed_Reader.Rows.Add(row2)
    Next
End Sub

Nota: No olvidemos usar las librerías

  • System.Net
  • System.Xml
  • System.IO
La Vista previa de la página queda así:
No se aceptan más comentarios