how to RSSFeeds from Multiple Websites

吃可爱长大的小学妹 提交于 2020-01-04 17:51:56

问题


i am gettting RssFeeds for my site and it is showing.But how to get RSS Feeds from multiple sites and need to show in line from 1st site three feeds,2nd site three feeds etc;mostly from CNN,BBC

here is my code :

protected void Page_Load(object sender, EventArgs e)
{
    BlogFeeds();
}
protected void BlogFeeds()
{
    try
    {
        XmlDocument xmldoc = new XmlDocument();
        XmlNodeList items = default(XmlNodeList);
        xmldoc.Load("http://rss.cnn.com/rss/edition_americas.rss");
        xmldoc.Load("http://feeds.bbci.co.uk/news/rss.xml?edition=int#");
        items = xmldoc.SelectNodes("/rss/channel/item");
        // use XPath to get only items
        string title = string.Empty;
        string link = string.Empty;
        string desc = string.Empty;
        string pubDesc = string.Empty;
        string st = "";
        int i = 0;

        foreach (XmlNode item1 in items)
        {
            foreach (XmlNode node1 in item1.ChildNodes)
            {
                if (node1.Name == "title")
                {
                    title = node1.InnerText;
                }
                if (node1.Name == "link")
                {
                    link = node1.InnerText;
                }
                if (node1.Name == "description")
                {
                    desc = node1.InnerText;
                    if (desc.Length > 90)
                    {
                        pubDesc = desc.Substring(0, 90);
                    }
                    else
                    { pubDesc = desc; }
                }
            }
            st += "<a target='_blank' href='" + link + "'>" + title + "</a><br />" + pubDesc + " ... " + "<div style='border-bottom: 1px dotted #84acfd; padding-top:10px;'></div></br>";
            i++;
            if (i == 3)
                break;
        }
        lblBlogOutput.Text = st;
    }
    catch (Exception eax)
    {
        return;
    }
}

}


回答1:


You cannot call .Load() and have it append to the XmlDocument.

Have a look at this modified version of the code:

  protected void Page_Load(object sender, EventArgs e)
        {
            BlogFeeds();
        }
        protected void BlogFeeds()
        {

            try
            {
                XmlDocument xmldoc = new XmlDocument();
                XmlNodeList items = default(XmlNodeList);

                xmldoc.Load("http://rss.cnn.com/rss/edition_americas.rss");
                items = xmldoc.SelectNodes("/rss/channel/item");
                ReadTopArticles(items);

                xmldoc.Load("http://feeds.bbci.co.uk/news/rss.xml?edition=int#");
                items = xmldoc.SelectNodes("/rss/channel/item");
                ReadTopArticles(items);

            }
            catch (Exception eax)
            {
                return;
            }
        }
        private void ReadTopArticles(XmlNodeList items)
        {
            string title = string.Empty;
            string link = string.Empty;
            string desc = string.Empty;
            string pubDesc = string.Empty;
            string st = "";
            int i = 0;

            foreach (XmlNode item1 in items)
            {
                foreach (XmlNode node1 in item1.ChildNodes)
                {
                    if (node1.Name == "title")
                    {
                        title = node1.InnerText;
                    }
                    if (node1.Name == "link")
                    {
                        link = node1.InnerText;
                    }
                    if (node1.Name == "description")
                    {
                        desc = node1.InnerText;
                        if (desc.Length > 90)
                        {
                            pubDesc = desc.Substring(0, 90);
                        }
                        else
                        { pubDesc = desc; }
                    }
                }
                st += String.Format("<a target='_blank' href='{0}'>{1}</a><br />{2} ... <div style='border-bottom: 1px dotted #84acfd; padding-top:10px;'></div></br>", link, title, pubDesc);
                i += 1;
                if (i == 3)
                    break;
            }
            lblBlogOutput.Text += st;
        }


来源:https://stackoverflow.com/questions/9109521/how-to-rssfeeds-from-multiple-websites

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!