阅读 Stack Overflow RSS 源
我正在尝试从提要获取未解答问题的列表,但我在阅读时遇到问题。
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();
我讨厌发布这样一个“请修复代码”的问题,但我真的被困住了。 我已经尝试了几个教程(有些给出编译错误)但没有帮助。 我认为我使用 XmlReader
和 XmlDocument
的方式是正确的,因为这是每个指南中的常见内容。
I'm trying to get a list of unanswered questions from the feed, but I am having trouble reading 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 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的枚举器
ienum
仅包含元素
元素。 由于该节点的名称不是entry
,因此不会向entries
添加任何内容。我猜您想迭代
元素的子节点。 请尝试以下操作:Your enumerator
ienum
contains only element, the<feed>
element. Nothing gets added toentries
since this node's name is notentry
.I'm guessing you want to iterate over the child nodes of the
<feed>
element instead. Try the following: