如何在asp.net中显示其他网站的RSS FEED到我的网站?

时间:2011-06-10 09:12:13

标签: c# .net asp.net rss feed

我想在我的ASP.NET网站上显示其他网站的RSS FEED。 我该怎么办?

4 个答案:

答案 0 :(得分:14)

<asp:DataList ID="DataList1" runat="server" DataSourceID="XmlDataSource1">
    <ItemTemplate>
        Title: <a href="<%# XPath("link") %>"><%# XPath("title") %></a><br />
        Pulish Date: <%# XPath("pubDate") %><br />
        Description: <%# XPath("description") %>
        <hr />
    </ItemTemplate>
</asp:DataList>

<asp:XmlDataSource ID="XmlDataSource1" Runat="server"
        DataFile="http://z.about.com/6/g/electrical/b/rss2.xml"
        XPath="rss/channel/item">
</asp:XmlDataSource>

答案 1 :(得分:1)

您可以向Feedburner服务添加外部Feed,并使用BuzzBoost服务将html代码嵌入到您的网页中。此代码将显示来自外部RSS源的最新帖子。

摘录示例:

<script src="http://feeds.feedburner.com/netrat-eu?format=sigpro" type="text/javascript" ></script><noscript><p>Subscribe to RSS headline updates from: <a href="http://feeds.feedburner.com/netrat-eu"></a><br/>Powered by FeedBurner</p> </noscript>

答案 2 :(得分:1)

使用此方法获取Feed,在此示例中,我将数据绑定到转发器控件以显示RSS Feed。

private void GetRSS()
{
    WebRequest rssReq = WebRequest.Create("URL");

    //Create a Proxy
    WebProxy px = new WebProxy("URL", 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[0];
        rssRepeater.DataBind();
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

答案 3 :(得分:1)

我自己遇到同样的问题并最终解决了。 使用此代码解决您的问题。

此代码是一个示例,但您需要在RSS中更改XML的URL和节点。

public static string GetRSS()
{
    try
    {
        XmlDocument newsUrl = new XmlDocument();
        newsUrl.Load("yoururl");
        XDocument doc = XDocument.Parse(newsUrl.InnerXml);
        var docs = doc.Root.Element("channel").ToString();
        newsUrl.LoadXml(docs);
        XmlNodeList idNodes = newsUrl.SelectNodes("channel/item");
        StringBuilder sb = new StringBuilder();
        int count = 0;
        count = idNodes.Count; 
        foreach (XmlNode node in idNodes)
        {
            sb.Append("<div><div><div><a href=" + node["nodename"].InnerText + ">" + node["nodename"].InnerText + "</a></div>");
            sb.Append("<div>" + node["nodename"].InnerText + "</div></div>");
            sb.Append("<div>" + node["nodename"].InnerText + "</div></div>");
           ........
        }
        return sb.ToString();          
    }
    catch (Exception ex)
    {
        throw ex;
    }