XML Reader threw Object Null exception, but node exists(?!)

Posted by Capt.Morgan on Stack Overflow See other posts from Stack Overflow or by Capt.Morgan
Published on 2012-06-19T09:02:55Z Indexed on 2012/06/19 9:16 UTC
Read the original article Hit count: 168

Filed under:
|
|
|

I am hoping someone could enlighten me as to why I am getting the annoying - "xml object reference not set to an instance .." error.

The elements (nodes?) I am looking for seem to exist and I have not misspelled it either :[

I might be doing something stupid here, but any help at all would be greatly appreciated.

My Code:

   private void button1_Click(object sender, RoutedEventArgs e)
{

    XmlDocument reader = new XmlDocument();
    reader.Load("Kotaku - powered by FeedBurner.xml");
    XmlNodeList titles = reader.GetElementsByTagName("title");
    XmlNodeList dates = reader.GetElementsByTagName("pubDate"); 
    XmlNodeList descriptions = reader.GetElementsByTagName("description");
    XmlNodeList links = reader.GetElementsByTagName("link"); 
    for (int i = 0; i < titles.Count; i++)
    {
        textBox1.AppendText(Environment.NewLine + titles[i].InnerText);
        textBox1.AppendText(Environment.NewLine + descriptions[i].InnerText); //<<-- Throws Object Ref Null Exception
        textBox1.AppendText(Environment.NewLine + links[i].InnerText);
        textBox1.AppendText(Environment.NewLine + dates[i].InnerText); //<<-- Throws Object Ref Null Exception

    }  

}

The XML I am using is a saved XML page from: http://feeds.gawker.com/kotaku/full

The way I am working on it now is as follows: I have saved the page from the above link (which is an XML page) and put it next to my EXE for easier access. Then I run the code.

© Stack Overflow or respective owner

Related posts about c#

Related posts about wpf