我正在尝试从the feed获取一个未回答的问题列表,但我在阅读它时遇到了问题。
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的方法是正确的,因为这是每个指南中常见的事情。
发布于 2009-02-01 22:52:22
枚举器ienum仅包含元素<feed>元素。由于此节点的名称不是entry,因此不会向entries添加任何内容。
我猜您希望遍历<feed>元素的子节点。尝试以下操作:
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();https://stackoverflow.com/questions/501643
复制相似问题