0
            try
            {
                rssDoc = new XmlDocument();
                // Load the XML context into XmlDocument
                rssDoc.Load(rssReader);
                MessageBox.Show(rssDoc.ToString());
            }
            catch (Exception ex)
            {
               errorProvider1.SetError(url, "Cannot load the RSS from this url");
            }
            // Loop for <rss> tag in xmldocument
            for (int i = 0; i < rssDoc.ChildNodes.Count; i++)
            {
                // If <rss> tag found
                if (rssDoc.ChildNodes[i].Name == "rss")
                {
                    // assign the <rss> tag node to nodeRSS
                    nodeRss = rssDoc.ChildNodes[i];
                }
            }
            //Loop for the <channel> tag in side <rss> tag stored in nodeRss
            for (int i = 0; i < nodeRss.ChildNodes.Count; i++)  <<<<<<EXCEPTION
            {
                // <channel> node found
                if (nodeRss.ChildNodes[i].Name == "channel")
                {
                    //assign the <channel> tag to nodeChannel 
                    nodeChannel = nodeRss.ChildNodes[i];
                }
            }

上記のコードは、ほとんどのrssフィードで正常に機能していますが、最後のループを実行しているときにnullrefrence例外が発生します。それを機能させるにはどうすればよいですか?

4

2 に答える 2

0

ループコードがtryブロック内にありません。最初にこれを変更してから、XDocumentとForEachを使用する必要があります。@MichaelKjörlingが書いたものも見てください。

try
{
    XDocument rssDoc = new XDocument(rssReader);

    foreach(var ele in rssDoc.Elemtens["rss"])
    {


    }
    foreach(var ele in rssDoc.Elemtens["channel"])
    {


    }
}    
catch (Exception ex)
{
    errorProvider1.SetError(url, "Cannot load the RSS from this url");
}
于 2012-11-22T15:14:01.253 に答える
0

なぜ車輪の再発明をするのですか?

XmlNode nodeChannel = rssDoc.SelectSingleNode("/rss/channel");

...トリックを行う必要があります。channel(RSSはルート要素内の単一の要素のみを許可すると確信していrssます。それ以外の場合は、SelectNodes()ではなく見てくださいSelectSingleNode()。)

于 2012-11-22T15:14:08.380 に答える