RSS阅读器部分读取节点

时间:2018-08-12 04:02:20

标签: c# rss

这是我一直在处理的一些代码,但它并未读取RSS feed中的所有XML节点。仅显示第一个节点:

将SyndicationFeed与使用系统服务模型一起使用

 public class RssNews
    {
        public string Title;
        public string Description;
        public string Publication;
    }

    private string RSS()
    {
        XmlDocument rssXmlDoc = new XmlDocument();
        rssXmlDoc.Load("http://www.hamqsl.com/solarrss.php");

        XmlNodeList rssNodes = rssXmlDoc.SelectNodes("rss/channel/item");
        StringBuilder rssContent = new StringBuilder();

        foreach (XmlNode rssNode in rssNodes)
        {
            XmlNode rssSubNode = rssNode.SelectSingleNode("title");
            string title = rssSubNode != null ? rssSubNode.InnerText : "";

            rssSubNode = rssNode.SelectSingleNode("item");
            string item = rssSubNode != null ? rssSubNode.InnerText : "";

            rssSubNode = rssNode.SelectSingleNode("description");
            string description = rssSubNode != null ? rssSubNode.InnerText : "";

            rssContent.Append("<a href='" + item + "'>" + title + "</a><br>" + description);


        }
        return rssContent.ToString();
    }
    private void btCurrentData_Click(object sender, RoutedEventArgs e)
    {
        tbRSS.Text = RSS();

    }

1 个答案:

答案 0 :(得分:0)

您可以看到完整的xml 例如在Firefox中使用视图源

view-source:http://www.hamqsl.com/solarrss.php

您正在阅读rss/channel/item。在此之下,只有一个title,因此您可以单独获取这些数据。

我假设您想阅读solardata,其路径为:rss/channel/item/solar/solardata

例如

var solarDataNodeList = rssXmlDoc.SelectNodes("rss/channel/item/solar/solardata");
if (solarDataNodeList != null && solarDataNodeList.Count > 0)
{
    var solarDataNode = solarDataNodeList[0]?.ChildNodes;
    if (solarDataNode != null)
    {
        foreach(XmlNode node in solarDataNode)
        {
            //deal with your node here
            //node.Name
            //node.InnerText
        }
    }
}

逻辑是,您查看实际的Xml,然后确定要在顶级选择的节点,然后查看其所有ChildNodes

完整的XML供参考:

<?xml version="1.0" encoding="UTF-8" ?> 
<rss version="0.91">
    <channel>
        <title>HAMQSL.com Solar Data RSS Feed</title> 
        <link>http://www.hamqsl.com/solarrss.php</link> 
        <description>Solar Data RSS Feed Updates</description> 
        <language>en-us</language> 
        <item>
            <title>HAMQSL.com Solar Data</title> 
            <solar>
                <solardata>
                    <source url="http://www.hamqsl.com/solar.html">N0NBH</source>
                    <updated> 12 Aug 2018 0508 GMT</updated>
                    <solarflux>67</solarflux>
                    <aindex> 10</aindex>
                    <kindex> 4</kindex>
                    <kindexnt>No Report</kindexnt>
                    <xray> A1.1   </xray>
                    <sunspots>0</sunspots>
                    <heliumline> 94.1</heliumline>
                    <protonflux>1.82e-01</protonflux>
                    <electonflux>2.82e+00</electonflux>
                    <aurora> 1</aurora>
                    <normalization>1.99</normalization>
                    <latdegree>67.5</latdegree>
                    <solarwind>392.2</solarwind>
                    <magneticfield>  0.2</magneticfield>
                    <calculatedconditions>
                        <band name="80m-40m" time="day">Poor</band>
                        <band name="30m-20m" time="day">Poor</band>
                        <band name="17m-15m" time="day">Poor</band>
                        <band name="12m-10m" time="day">Poor</band>
                        <band name="80m-40m" time="night">Fair</band>
                        <band name="30m-20m" time="night">Poor</band>
                        <band name="17m-15m" time="night">Poor</band>
                        <band name="12m-10m" time="night">Poor</band>
                    </calculatedconditions>
                    <calculatedvhfconditions>
                        <phenomenon name="vhf-aurora" location="northern_hemi">Band Closed</phenomenon>
                        <phenomenon name="E-Skip" location="europe">Band Closed</phenomenon>
                        <phenomenon name="E-Skip" location="north_america">Band Closed</phenomenon>
                        <phenomenon name="E-Skip" location="europe_6m">Band Closed</phenomenon>
                        <phenomenon name="E-Skip" location="europe_4m">Band Closed</phenomenon>
                    </calculatedvhfconditions>
                    <geomagfield>ACTIVE</geomagfield>
                    <signalnoise>S3-S4</signalnoise>
                    <fof2> 4.4</fof2>
                    <muffactor> 3.11</muffactor>
                    <muf>13.68</muf>
                </solardata>
            </solar>
        </item>
    </channel>
</rss>