0

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

4

1 回答 1

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;
    }
}
于 2012-05-20T08:41:19.050 回答