Reading RSS feeds

I'm trying to get a list of unanswered questions from a feed , but it's hard for me to read it.

const string RECENT_QUESTIONS = "https://stackoverflow.com/feeds"; XmlTextReader reader; XmlDocument doc; // Load the feed in reader = new XmlTextReader(RECENT_QUESTIONS); //reader.MoveToContent(); // Add the feed to the document doc = new XmlDocument(); doc.Load(reader); // Get the <feed> element XmlNodeList feed = doc.GetElementsByTagName("feed"); // Loop through each item under feed and add to entries IEnumerator ienum = feed.GetEnumerator(); List<XmlNode> entries = new List<XmlNode>(); while (ienum.MoveNext()) { XmlNode node = (XmlNode)ienum.Current; if (node.Name == "entry") { entries.Add(node); } } // Send entries to the data grid control question_list.DataSource = entries.ToArray(); 

I don’t like posting the question β€œplease correct the code”, but I am really stuck. I tried several tutorials (some of them give compilation errors), but without help. I assume that I will go right using XmlReader and XmlDocument , as this was common in every tutorial.

+1
c # xml xmlreader xmldocument feeds
source share
1 answer

Your ienum counter contains only the element, the <feed> element. Nothing is added to entries since this node name is not entry .

I assume that you want to iterate over the child nodes of the <feed> element. Try the following:

 const string RECENT_QUESTIONS = "http://stackoverflow.com/feeds"; XmlTextReader reader; XmlDocument doc; // Load the feed in reader = new XmlTextReader(RECENT_QUESTIONS); //reader.MoveToContent(); // Add the feed to the document doc = new XmlDocument(); doc.Load(reader); // Get the <feed> element. XmlNodeList feed = doc.GetElementsByTagName("feed"); XmlNode feedNode = feed.Item(0); // Get the child nodes of the <feed> element. XmlNodeList childNodes = feedNode.ChildNodes; IEnumerator ienum = childNodes.GetEnumerator(); List<XmlNode> entries = new List<XmlNode>(); // Iterate over the child nodes. while (ienum.MoveNext()) { XmlNode node = (XmlNode)ienum.Current; if (node.Name == "entry") { entries.Add(node); } } // Send entries to the data grid control question_list.DataSource = entries.ToArray(); 
+4
source share

All Articles