I'm trying to get a list of unanswered questions from the feed, but I am having trouble reading it.
我正在尝试从Feed中获取未回答的问题列表,但我无法阅读它。
const string RECENT_QUESTIONS = "https://*.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 hate to post such a "please fix the code" question, but I'm really stuck. I've tried several tutorials (some giving compile errors) but to no help. I assume I'm going the right way using an XmlReader
and an XmlDocument
as that's been a common thing from each guide.
我讨厌发布这样一个“请修复代码”的问题,但我真的被卡住了。我已经尝试了几个教程(一些给出编译错误),但没有帮助。我假设我正在使用XmlReader和XmlDocument以正确的方式,因为这是每个指南中常见的事情。
1 个解决方案
#1
Your enumerator ienum
contains only element, the <feed>
element. Nothing gets added to entries
since this node's name is not entry
.
您的枚举器ienum仅包含元素
I'm guessing you want to iterate over the child nodes of the <feed>
element instead. Try the following:
我猜你想要迭代
const string RECENT_QUESTIONS = "http://*.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();
#1
Your enumerator ienum
contains only element, the <feed>
element. Nothing gets added to entries
since this node's name is not entry
.
您的枚举器ienum仅包含元素
I'm guessing you want to iterate over the child nodes of the <feed>
element instead. Try the following:
我猜你想要迭代
const string RECENT_QUESTIONS = "http://*.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();