使用ASP.Net获取和显示RSS源

时间:2014-07-04 10:43:24

标签: c# javascript html asp.net rss

我需要阅读并在我的网页上显示此Feed。

http://picasaweb.google.com/data/feed/base/user/ComunidadMexicana?alt=rss&kind=album&hl=it&access=public

使用c#net 2.

我尝试了这个教程:

http://www.aspsnippets.com/Articles/Fetch-and-Display-RSS-Feeds-using-ASP.Net.aspx

但错误是:

A column named 'link' already belongs to this DataTable: 
cannot set a nested table name to the same name.

为什么?

我的代码如下。

我非常感谢您在解决这个问题时能给我的任何帮助。

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Repeater ID="rssRepeater" runat="server">
            <ItemTemplate>
                <table style="border: solid 1px black; width: 500px; font-family: Arial">
                    <tr>
                        <td style="font-weight: bold">
                            <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%#Eval("link")%>' Text='<%#Eval("title")%>'></asp:HyperLink>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <hr />
                        </td>
                    </tr>
                    <tr>
                        <td style="background-color: #C2D69B">
                            <asp:Label ID="Label1" runat="server" Text='<%#Eval("description")%>'></asp:Label>
                        </td>
                    </tr>
                </table>
                <br />
            </ItemTemplate>
        </asp:Repeater>
    </div>
    </form>
</body>
</html>


using System;
using System.Net;
using System.Xml;
using System.Data;

public partial class FeedPicasa_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        GetRSS();
    }

    private void GetRSS()
    {
        //Create a WebRequest
        WebRequest rssReq =
        WebRequest.Create("http://picasaweb.google.com/data/feed/base/user/ComunidadMexicana?alt=rss&kind=album&hl=it&access=public");

        //Create a Proxy
        WebProxy px = new WebProxy("http://picasaweb.google.com/data/feed/base/user/ComunidadMexicana?alt=rss&kind=album&hl=it&access=public", true);

        //Assign the proxy to the WebRequest
        rssReq.Proxy = px;

        //Set the timeout in Seconds for the WebRequest
        rssReq.Timeout = 5000;
        try
        {
            //Get the WebResponse
            WebResponse rep = rssReq.GetResponse();

            //Read the Response in a XMLTextReader
            XmlTextReader xtr = new XmlTextReader(rep.GetResponseStream());

            //Create a new DataSet
            DataSet ds = new DataSet();

            //Read the Response into the DataSet
            ds.ReadXml(xtr);

            //Bind the Results to the Repeater
            rssRepeater.DataSource = ds.Tables[2];
            rssRepeater.DataBind();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}

1 个答案:

答案 0 :(得分:2)

如何使用纯Javascript / jQuery解决方案如下:

<强>的Javascript

$(function(){
        url = 'http://picasaweb.google.com/data/feed/base/user/ComunidadMexicana?alt=rss&kind=album&hl=it&access=public';
        $.ajax({
        type: "GET",
        url: document.location.protocol + '//ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=1000&callback=?&q=' + encodeURIComponent(url),
        dataType: 'json',
        error: function(){
            alert('Unable to load feed, Incorrect path or invalid feed');
        },
        success: function(xml){
            values = xml.responseData.feed.entries;

            $.each(values, function( index, value ) {
              $('#myFeed').append(value.content);
                $('#myFeed').append('<br/>');
            });
        }
    });
    });

<强> HTML

<div id="myFeed"/>

工作小提琴

http://jsfiddle.net/5EtnX/1/

<强>描述

上面简单地对feed进行json调用,在success函数内部循环结果并使用jQuery append输出feed的内容。

相关问题