Ros*_*oss 2 c# xml xmldocument xmlreader 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();
Run Code Online (Sandbox Code Playgroud)
我讨厌发布这样一个“请修复代码”的问题,但我真的被卡住了。我已经尝试了几个教程(有些给出了编译错误)但没有帮助。我认为我使用 anXmlReader
和 an 的方式是正确的,XmlDocument
因为这在每个指南中都是很常见的。
您的枚举器ienum
仅包含元素,即<feed>
元素。entries
由于此节点的名称不是 ,因此不会添加任何内容entry
。
我猜你想迭代<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();
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4470 次 |
最近记录: |