在ASP.NET中创建RSS阅读器

时间:2012-05-20 07:04:32

标签: asp.net rss-reader

我使用VS2010,C#我想从其他网站读取RSS内容并在我的网站上显示,目前这个过程是手动执行的(网站管理员搜索其他网站并在新闻项目中复制/粘贴相关内容!),但我想要自动完成它,我认为最好的解决方案是RSS,我已经尝试了几个示例代码,但没有一个有效,是否有一种简单的方法在ASP.NET中实现RSS阅读器?我有什么选择?

1 个答案:

答案 0 :(得分:1)

阅读RSS链接

public void ReadDoc(XmlDocument rssDoc)
{
    XmlNode nodeRss = null;
    XmlNode nodeChannel = null;
    XmlNode nodeItem = null;
    try
    {
        if (rssDoc == null)
        {
            return;
        }

        // Loop for the <rss> tag
        for (int i = 0; i < rssDoc.ChildNodes.Count; i++)
        {
            // If it is the rss tag
            if (rssDoc.ChildNodes[i].Name == "rss")
            {
                 nodeRss = rssDoc.ChildNodes[i];
                 break;
            }
        }

        if (nodeRss == null)
        {
            return;
        }
        for (int i = 0; i < nodeRss.ChildNodes.Count; i++)
        {
            if (nodeRss.ChildNodes[i].Name == "channel")
            {
                nodeChannel = nodeRss.ChildNodes[i];
                break;
            }
        }

        if (nodeChannel == null)
        {
            return;
        }

        // Loop for the <title>, <link>, <description> and all the other tags
        for (int i = 0; i < nodeChannel.ChildNodes.Count; i++)
        {
            if (nodeChannel.ChildNodes[i].Name == "item")
            {
                nodeItem = nodeChannel.ChildNodes[i];
                if (nodeItem["title"] != null){}
                if (nodeItem["description"] != null){}
                if (nodeItem["pubDate"] != null){}
            }
        }
    }
    catch (Exception)
    {
        throw;
    }
    finally
    {
        nodeRss = null;
        nodeChannel = null;
        nodeItem = null;
    }
}

public void CreateRSS(String Path)
{
    XmlDocument doc = null;
    XmlTextReader rssReader = null;
    Label doclbl = null;
    Label snolbl = null;
    try
    {
        try
        {
            rssReader = new XmlTextReader(Path);
        }
        catch (Exception)
        {
            return;
        }

        if (rssReader == null)
        {
            return;
        }

        doc = new XmlDocument();
        try
        {
            doc.Load(rssReader);
        }
        catch (Exception)
        {
            return;
        }

        if (doc == null)
        {
            return;
        }

        ReadDoc(doc);
    }
    catch (Exception)
    {
        throw;
    }
    finally
    {
        doc = null;
        rssReader = null;
        doclbl = null;
        snolbl = null;
    }
}