ASP.Net RssToolkit Version 2.0

 

I have been part of the team working on the latest version ASPNET RssToolkit, originally created by Dmitry Robsman. We enhanced this awesome Toolkit and have just released version 2.0 of the Toolkit. Please check out Codeplex to download this toolkit.

Here are some of the changes in this version –

  • Consume and publish Atom/Rdf/Opml feeds.
  • Strongly typed classes, close to Rss specification.
  • Feeds Aggregation using OPML. The outlines in OPML can have Rss/Atom/Rdf.
  • Rss Schema validation during aggregation.
  • Download Manager can be used to download any Url and return a Stream. It also has inbuilt caching mechanism. 
  • Added support for Extensions and Enclosures.
  • Added Visual Studio Testing Framework Unit Tests.
  • Rearranged Namespaces to closely match the features.

Here is how to use the RssToolkit. I have taken excerpts from ScottGu’s review on the original RssToolkit.

Consuming Feeds using RssToolkit -

A.      Using RssDataSource – This webcontrol gives you the ability to databind to an Rss/Atom/Rdf/Opml feed. It inherits from DataSourceControl to give itself the ability serve as a data source to data-bound controls. Here are the steps you can take to use RssDataSource in your page or user control –

1.       Add the RssDataSource and RssHyperLink to the Toolbox in Visual Studio.

 

 

2.       In your Web form, drop the RssDataSource.

3.       Click the control and “Configure Data Source”

 

 

4.       It ask you for the feed Url. The RssDataSourceConfigForm.cs is used in this case.

You can provide RSS/ATOM/RDF/OPML and RssDataSource will automatically identify the feed type and provide Databinding capability.

As you can see below, I’m actually providing an Atom feed URL to the RssDataSource.

 

 

5.       You can then bind a control like Gridview to this DataSource.

 

 

6.       I will “Edit Columns” on the GridView and have 2 columns, as shown –

 

 

7.       You can also specify the number of items you want to be returned back. I will go in the code behind and change that to 5

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<%@ Register Assembly="RssToolkit" Namespace="RssToolkit.Web.WebControls" TagPrefix="cc1" %>

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>Consuming Google News using RssToolkit</title>

</head>

<body>

    <form id="form1" runat="server">

    Google News (Atom Format)

    <div>

        <cc1:rssdatasource id="RssDataSource1" runat="server" maxitems="5" url="http://news.google.com/?output=atom"></cc1:rssdatasource>

    </div>

        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="RssDataSource1">

            <Columns>

                <asp:HyperLinkField DataNavigateUrlFields="link" DataTextField="link" HeaderText="Link"/>

                <asp:BoundField DataField="title" HeaderText="Title" SortExpression="title" />

            </Columns>

        </asp:GridView>

    </form>

</body>

</html>

 

8.       Compile and View the page in browser –

 

 

B.      Consume using RssDocument.cs – RssDocument.cs class can be used from your code if you want to consume/publish or manipulate feeds. It provides various overloads for Loading a feed –

 

 

·         Load - takes 3 overloads System.Uri, Xml string and XmlReader. You can provide Rss/Atom/Rdf/Opml type of formats for these 3 overloads and it will automatically identify the base feed type and construct the class.

·         SelectItems – this method returns IEnumerable which gives the ability to iterate over the collection. It is also used internally to provide data binding capability to RssDataSource.

·         ToXml – this method which returns an Xml as a string, provides the capability of publishing the feed into Rss/Atom/Rdf/Opml formats. The input parameter (DocumentType enum) suggests the format of Xml returned by this method.

·         ToDataSet – this method returns the feed as a DataSet.

Here is how to use the RssDocument –

void Page_Load(object sender, EventArgs e)

 {

    RssToolkit.Rss.RssDocument rss = RssToolkit.Rss.RssDocument.Load(new System.Uri("http://rss.msnbc.msn.com/id/3032091/device/rss/rss.xml"));

    Image1.ImageUrl = rss.Channel.Image.Url;

    GridView1.DataSource = rss.SelectItems();

    GridView1.DataBind();

 }

C.      Consume using Custom Generated Class and RssDl.exe – Let’s say you want to consume a feed which has some extensions that is not specified in the Rss specification such as Media Rss by Yahoo.
An example of Media Rss tags are -
<media:player url="http://youtube.com/?v=dMH0bHeiRNg" />
<media:thumbnail url="http://sjc-static6.sjc.youtube.com/vi/dMH0bHeiRNg/2.jpg" width="120" height="90" />
<media:title>Evolution of Dance</media:title>
<media:category label="Tags">Dancing comedy</media:category>
<media:credit>judsonlaipply</media:credit>

You can still get strong typing for these extensions by generating a custom class using the RssDl.exe

 

Usage – RssDl.exe “URL” “ClassName.cs”

E.g.

RssDl.exe “http://youtube.com/rss/global/top_favorites.rss” “YouTube.cs”

RssDl.exe “http://news.google.com/?output=rss” “GoogleNews.vb”

  As shown below I created a custom class for YouTube’s Top favorites feed, which has Media Rss tags.

 

 

                If you open the newly created class “YouTube.cs” you will see the extensions as Strongly typed properties inside the class.

    [XmlElement("player", Namespace="http://search.yahoo.com/mrss/")]

    public YoutubePlayer Player {

        get {

            return _player;

        }

        set {

            _player = value;

        }

    }

   

    [XmlElement("thumbnail", Namespace="http://search.yahoo.com/mrss/")]

    public YoutubeThumbnail Thumbnail {

        get {

            return _thumbnail;

        }

        set {

            _thumbnail = value;

        }

    }

   

    [XmlElement("category", Namespace="http://search.yahoo.com/mrss/")]

    public YoutubeCategory Category {

        get {

            return _category;

        }

        set {

            _category = value;

        }

    }

   

    [XmlElement("credit", Namespace="http://search.yahoo.com/mrss/")]

    public string Credit {

        get {

            return _credit;

        }

        set {

            _credit = value;

        }

    }

 

 

You will have to embed this class in your application or put in App_Code folder of your website. You can then use the class just like RssDocument.cs.

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>Youtube Media Rss</title>

 

    <script language="c#" runat="server">

        void Page_Load(object sender, EventArgs e)

        {

            YoutubeRss rss = YoutubeRss.Load();

            DataList1.DataSource = rss.Channel.Items;

            DataList1.DataBind();

        }

    </script>

 

</head>

<body>

    <form id="form1" runat="server">

        You Tube Top Favories(Media Rss)

        <asp:DataList ID="DataList1" runat="server" RepeatColumns="3" GridLines="both">

            <ItemTemplate>

                <asp:Image ID="Image1" runat="Server" ImageUrl='<%# Eval("Thumbnail.Url")  %>' /><br />

                        <asp:HyperLink ID="HyperLink1" NavigateUrl='<%# Eval("Player.Url") %>' Text='Link' runat="server" />

            </ItemTemplate>

        </asp:DataList>

    </form>

</body>

</html>

 

Here is what the page looks like -

 

 

Aggregation

RssToolkit provides Aggregation using Outline Processor Markup Language (OPML). Check out the OPML specification here. OPML is used for creating a list of feeds known as Outlines to be shared or aggregated. Here is what an OPML format looks like –

<?xml version="1.0" encoding="iso-8859-1"?>

<opml version="1.1">

  <head>

    <title>List of Feeds News Feeds</title>

    <dateCreated>Tue, 11 Nov 2003 19:47:24 GMT</dateCreated>

    <dateModified>Tue, 11 Nov 2003 19:47:28 GMT</dateModified>

    <ownerName>Webmaster</ownerName>

    <ownerEmail>owner@msdn.com</ownerEmail>

    <expansionState></expansionState>

    <vertScrollState>3</vertScrollState>

    <windowTop>93</windowTop>

    <windowLeft>127</windowLeft>

    <windowBottom>585</windowBottom>

    <windowRight>710</windowRight>

  </head>

  <body>

    outline text='BBC News’ description='Updated every minute of every day - FOR PERSONAL USE ONLY' htmlUrl='http://news.bbc.co.uk/go/click/rss/0.91/public/-/1/hi/default.stm' language='unknown' title='BBC News' type='rss' version='RSS' xmlUrl='http://news.bbc.co.uk/rss/newsonline_uk_edition/front_page/rss091.xml'/>

    <outline text='CNET News.com' description='Tech news and business reports by CNET News.com. Focused on information technology, core topics include computers, hardware, software, networking, and Internet media.' htmlUrl='http://news.com.com/' language='unknown' title='CNET News.com' type='rss' version='RSS2' xmlUrl='http://news.com.com/2547-1_3-0-5.xml'/>

  </body>

</opml>

 

As seen above the Outline has the information of the feeds to be aggregated, XmlUrl provides the Url of the Feed.

To aggregate Feeds using RssToolkit you need to provide the location of the OPML file or proved the OPML Xml. You can use the Load method in the RssDocument.cs Class for Aggregation. As shown before the Load of RssDocument has 3 overloads –

public static RssDocument Load(string xml);
public static RssDocument Load(Uri url);
public static RssDocument Load(XmlReader reader);

 

Example -

RssDocument rssAggregatedFeed = RssDocument.Load (new System.Uri("http://static2.podcatch.com/blogs/gems/opml/mySubscriptions.opml"));

Publishing your Feeds

 

RssHyperLink – This WebControl can be used for publishing your Feed. You can point this tool to the feed location and it automatically generates link tag -

<link rel="alternate" type="application/rss+xml" title="Rss" href="~/RssHyperLink.ashx" />

 

This tag enables Feed Autodiscovery in browsers. It enables the  in IE and FireFox. To publish the feeds using RssToolkit, you can use one of the mechanisms below –

 

A.       Using ASP.Net Buildprovider mechanism – RssToolkit uses ASP.Net BuildProvider class to automatically detect certain files created or modified in the filesystem. It automatically generates code and compiles the code for any files with extension *.rss or *.rssdl. RssToolkit then uses RssDocument.cs class and De-serializes the feed. It also builds an HttpHandler, which you can then inherit from to publish your feed.

In the samples provided. The App_Code folder has Sample5.Rss

<?xml version="1.0" encoding="utf-8"?>

<rss version="2.0">

    <channel>

        <title>Sample Channel</title>

        <link>~/RssHyperlinkFromCustomClass.aspx</link>

        <description>Channel For Scenario5 in ASP.NET RSS Toolkit samples.</description>

        <ttl>10</ttl>

        <name></name>

        <user></user>

        <pubDate>Tue, 10 Apr 2007 23:01:10 GMT</pubDate>

        <lastBuildDate>Tue, 10 Apr 2007 23:01:10 GMT</lastBuildDate>

        <webMaster>webmaster@email.com</webMaster>

        <item>

            <title></title>

            <description></description>

            <link></link>

        </item>

        <item>

            <title></title>

            <description></description>

            <link></link>

        </item>

    </channel>

</rss>

When the file is created or modified RssToolkit automatically generates code and compiles it based on the schema of this file. It pre-fixes all the files with “Sample5”. So like RssDocument there will be Sample5Rss, RssChannel will have Sample5Channel and so on. It also creates a default HttpHanlder called Sample5HtppHandlerBase which inherits from RssHttpHandlerBase. You can create a custom *.ashx HttpHandler which inherits from Sample5HttpHandlerBase and you can publish the Sample5.rss

<%@ WebHandler Language="C#" Class="RssHyperLinkFromCustomClass" %>

 

using System;

using System.Collections.Generic;

using System.Web;

using RssToolkit.Rss;

 

public class RssHyperLinkFromCustomClass: Sample5HttpHandlerBase

{

    protected override void PopulateRss(string rssName, string userName)

    {

        Rss.Channel = new Sample5Channel();

       

        Rss.Channel.Items = new List<Sample5Item>();

        if (!string.IsNullOrEmpty(rssName))

        {

            Rss.Channel.Title += " '" + rssName + "'";

        }

 

        if (!string.IsNullOrEmpty(userName))

        {

            Rss.Channel.Title += " (generated for " + userName + ")";

        }

 

        Rss.Channel.Link = "~/scenario6.aspx";

        Rss.Channel.Description = "Channel For Scenario6 in ASP.NET RSS Toolkit samples.";

        Rss.Channel.Ttl = "10";

        Rss.Channel.User = userName;

 

 

        Sample5Item item = new Sample5Item();

        item.Title = "CodeGeneratedClass";

        item.Description = "Consuming RSS feed programmatically using strongly typed classes";

        item.Link = "~/CodeGeneratedClass.aspx";

        Rss.Channel.Items.Add(item);

 

        item = new Sample5Item();

        item.Title = "ObjectDataSource";

        item.Description = "Consuming RSS feed using ObjectDataSource";

        item.Link = "~/ObjectDataSource.aspx";

        Rss.Channel.Items.Add(item);

    }   

}

You can use RssHyperLink to point to this Custom HttpHandler –

<cc1:RssHyperLink ID="Rsshyperlink4" runat="server" IncludeUserName="True" NavigateUrl="~/RssHyperLinkFromCustomClass.ashx">RSS</cc1:RssHyperLink>

When clicked on the link the output shown will be –

 

B.      Using RssDocument class – RssDocument.cs has a method ToXml(), which can be used to serialize and publish the RssDocument class.

public string ToXml(DocumentType outputType);

 

It takes DocumentType enum which has the following values –

public enum DocumentType
{
    Unknown,
    Rss,
    Opml,
    Atom,
    Rdf

}

 

So to publish your feed as an ATOM from RssDocument, you can do the following inside an ASP.Net Module or an HttpHandler –

string inputXml = rssDocument.ToXml(DocumentType.Atom); // Publish as Atom

XmlDocument document = new XmlDocument();
document.LoadXml(inputXml);

            context.Response.ContentType = "text/xml";

// save XML into response

            document.Save(HttpContent.Current.Response.OutputStream);

 

In the samples provided with the Toolkit you can see 2 samples which publish your feed into various formats. The output is as shown

 

As seen in the above sample, the output feed is picked up from the querystring “outputtype”. You can provide “rss”, “rdf”, “atom” and “opml” for the respective feeds.

Strongly typed classes –

The toolkit provides strong typing to the Rss Classes.

e.g.

void Page_Load(object sender, EventArgs e)

 {

    RssToolkit.Rss.RssDocument rss = RssToolkit.Rss.RssDocument.Load(new System.Uri("http://rss.msnbc.msn.com/id/3032091/device/rss/rss.xml"));

    Image1.ImageUrl = rss.Channel.Image.Url;

    GridView1.DataSource = rss.SelectItems();

    GridView1.DataBind();

 }

 

Caching of Feeds –

DownloadManager.cs class is used to get information of the feed. The feed is cached locally to the disk. The location and time of the caching is configurable. You can use the keys in the web.config to change the values –

<appSettings>

        <add key="defaultRssTtl" value="10"/>

        <add key="rssTempDir" value="c:/temp"/>

</appSettings>

时间: 2024-10-25 15:43:09

ASP.Net RssToolkit Version 2.0的相关文章

未能加载文件或程序集“EnvDTE, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”或它的某一个依赖项。系统找不到指定的文件。

问题描述 未能加载文件或程序集"EnvDTE,Version=8.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a"或它的某一个依赖项.系统找不到指定的文件.说明:执行当前Web请求期间,出现未处理的异常.请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息.异常详细信息:System.IO.FileNotFoundException:未能加载文件或程序集"EnvDTE,Version=8.0.0.0

类型“UCMLCommon.UCMLBPObject”在未被引用的程序集中定义。必须添加对程序集“Workflow, Version=2.0.0.606, Cu

问题描述 如题,我有一个ASP.NET项目,用C#开发,其中有一个DLL文件,我反编译了,然后重新引用,编译时提示:错误143类型"UCMLCommon.UCMLBPObject"在未被引用的程序集中定义.必须添加对程序集"Workflow,Version=2.0.0.606,Culture=neutral,PublicKeyToken=505ffed16bbd71d1"的引用.不知如何解决,请高手指点! 解决方案 解决方案二: 解决方案三:无人回复,自己顶!解决方

请求“System.Data.SqlClient.SqlClientPermission,System.Data,Version=2.0.0.0,Culture=neutral,PublickeyToken=b77a5c561934e089”类型的权限失败

问题描述 碰到这样一个异常"请求"System.Data.SqlClient.SqlClientPermission,System.Data,Version=2.0.0.0,Culture=neutral,PublickeyToken=b77a5c561934e089"类型的权限已失败请高手给予解决,谢谢!加急 解决方案 解决方案二:这个问题,我也是刚刚遇到的,解决办法:1.先看看你这个项目文件夹下,属性中安全,当前用户是否有权限.添加上就可以了.解决方案三:按照楼上的试了,

未能加载文件或程序集“DreamweaverCtrls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=83

问题描述 我在用DW做毕业设计,在预览页面时出现如下问题,请各位高手帮帮忙"/xywz"应用程序中的服务器错误.--------------------------------------------------------------------------------分析器错误说明:在分析向此请求提供服务所需资源时出错.请检查下列特定分析错误详细信息并适当地修改源文件.分析器错误信息:未能加载文件或程序集"DreamweaverCtrls,Version=1.0.0.0,

asp.net初学者:petshop4.0设计说明

asp.net|初学|设计 petshop4.0设计说明 一.项目名称及描述:(实现步骤为:4-3-6-5-2-1)  1.WEB=表示层 2.BLL=业务逻辑层  3.IDAL=数据访问层接口定义  4.Model=业务实体 5.DALFactory=数据层的抽象工厂(创建反射) 6.SQLServerDAL=SQLServer数据访问层 / OracleDAL=Oracle数据访问层 DBUtility 数据库访问组件基础类  二.项目引用关系  1.Web 引用 BLL. 2.BLL 引用

Digital Unix Version 4.0下Oracle 8.0.5服务器的安装

oracle|unix|服务器 Digital Unix Version 4.0下Oracle 8.0.5服务器的安装 石骁騑(中软网络技术股份有限公司,北京,100081) 一. 系统需求在安装Oracle 8 服务器前,首先检查系统是否满足表1和表2所示的软硬件需求.1. 硬件需求 表1 硬件需求硬件项目 需求CPU Digital Unix alpha 系统内存 最少128M RAM交换空间 2-4倍的内存大小磁盘驱动(Disk Drives) 至少四个设备:一个用于Oracle软件的分发

Microsoft Visual J#.NET (JSharp) Version 7.0 Beta 1 out

js|visual Thanks Charles and Activewin for this news. Microsoft has posted it's Beta 1 version of Microsoft J#.Net Version 7.0 Beta 1 for download, it hasn't been posted on Download Center yet and the file is dated October 11, so it is unclear if thi

未能加载文件或程序集System.Data,Version=2.0.0.0

  sqlserver 2005打开出现无法正常访问数据,提示信息: 未能加载文件或程序集"System.Data,Version=2.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089"或它的某一个依赖项.系统找不到指定的文件. 解决方法: 打开"C:WINDOWSassembly"文件夹,查看有没有名称为"System.Data".版本号.公钥标记与错误提示中的Version.Publi

跟我一起学习ASP.NET 4.5 MVC4.0(二)

原文http://www.cnblogs.com/xdotnet/archive/2012/03/06/aspnet_mvc40_keywords.html 上一篇文章中(跟我一起学习ASP.NET 4.5 MVC4.0(一)) 我们基础的了解了一下ASP.NET MVC4.0的一些比较简单的改变,主要是想对于MVC3.0来说的.因为这一些列主要是要给ASP.NET MVC初学者,或者还没有使用过MVC的ASP.NET开发者进行培训学习,当然也可以让我温习一下这种开发模式.所以本篇不得不讲解一下