简体   繁体   中英

How to display RSS FEED of other website to my site in asp.net?

I want to display RSS FEED of other website in my site made in ASP.NET. what should i do?

<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>

I myself get in a same problem and fixed finally. Use this code to solve your problem.

This code is an example but you are required to change your url and nodes of the XML in the RSS.

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;
    }

You can add external feed to Feedburner service and use BuzzBoost service to have a html code to embed to your page. This code will show the latest posts from external RSS feed.

Example of snippet:

<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>

Use this method to get to get feed, in this example, I am binding the Data to repeater control to show the 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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM