Reputation: 607
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.
Upvotes: 0
Views: 387
Reputation: 8832
It cannot run in for loop because number of items in titles, descriptions, links and dates was (at the time when I ran it) respectively: 39 38 39 37.
Upvotes: 1
Reputation: 63065
The way that you read RSS is wrong. First get all the items and loop though each item and build the text. Better if you can use StringBuilder
and finally convert it to string.
Before access properties of an object you can check for null.
XmlDocument RSSXml = new XmlDocument();
RSSXml.Load("Kotaku - powered by FeedBurner.xml");
XmlNodeList RSSNodeList = RSSXml.SelectNodes("rss/channel/item");
StringBuilder sb = new StringBuilder();
foreach (XmlNode RSSNode in RSSNodeList)
{
XmlNode RSSSubNode;
RSSSubNode = RSSNode.SelectSingleNode("title");
string title = RSSSubNode != null ? RSSSubNode.InnerText : "";
RSSSubNode = RSSNode.SelectSingleNode("link");
string link = RSSSubNode != null ? RSSSubNode.InnerText : "";
RSSSubNode = RSSNode.SelectSingleNode("description");
string desc = RSSSubNode != null ? RSSSubNode.InnerText : "";
RSSSubNode = RSSNode.SelectSingleNode("pubDate");
string pubDate = RSSSubNode != null ? RSSSubNode.InnerText : "";
sb.Append("<font face='arial'><p><b><a href='");
sb.Append(link);
sb.Append("'>");
sb.Append(title);
sb.Append("</a></b><br/>");
sb.Append(desc);
sb.Append(pubDate);
sb.Append("</p></font>");
}
textBox1.Text = sb.ToString();
Upvotes: 4
Reputation: 1161
I checked the source of http://feeds.gawker.com/kotaku/full.
What I see - in the HMTL source at least - there is a <title>
element in the channel header too. Which not belongs to any item yet. Isn't it possible, that your code count some additional elements like this when you use title.Count
as the limit of your for
cicle?
If that is the case, it is possible, that you wont have enough elements in the other arrays in your loop. Did you check this too?
Upvotes: 0