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

有帮助吗?

解决方案

读取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;
    }
}
.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top