我正在为我的站点获取RssFeeds,但它正在显示。但是如何从多个站点获取RSS Feed,并且需要从第一站点的三个站点,第二站点的三个站点等依次显示;主要是从CNN,BBC

这是我的代码:

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


}

最佳答案

您不能调用.Load()并将其附加到XmlDocument。

看一下这段修改后的代码:

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

关于c# - 如何从多个网站的RSS提要,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9109521/

10-10 10:38