XmlTextReader - 如何迭代节点
我需要使用 XmlTextReader 循环浏览 XML 文档节点的帮助。不幸的是,除了 XmlTextReader 之外,使用其他任何东西都不是一个选择。
我的代码: <代码>
class Program { private static void Main(string[] args) { XmlTextReader reader = new XmlTextReader("http://api.own3d.tv/liveCheck.php?live_id=180491"); while (reader.Read()) { switch (reader.NodeType) { case XmlNodeType.Text: Console.WriteLine("Live: " + reader.Value); break; } } Console.ReadLine(); } }
<代码>
使用的 XML:
<own3dReply>
<liveEvent>
<isLive>true</isLive>
<liveViewers>225</liveViewers>
<liveDuration>1222</liveDuration>
</liveEvent>
</own3dReply>
它输出到控制台的内容:
Live: true Live: 225 Live: 1222
它需要输出的内容:
Live: true Viewers: 225 Duration: 1222
它需要迭代每个节点并执行此操作,但我无法弄清楚。我尝试使用 switch 和 while 语句,但我似乎无法让它工作。
I need help with cycling through nodes of a XML document using XmlTextReader. Using anything else besides XmlTextReader is not an option, unfortunately.
My code:
class Program { private static void Main(string[] args) { XmlTextReader reader = new XmlTextReader("http://api.own3d.tv/liveCheck.php?live_id=180491"); while (reader.Read()) { switch (reader.NodeType) { case XmlNodeType.Text: Console.WriteLine("Live: " + reader.Value); break; } } Console.ReadLine(); } }
XML used:
<own3dReply>
<liveEvent>
<isLive>true</isLive>
<liveViewers>225</liveViewers>
<liveDuration>1222</liveDuration>
</liveEvent>
</own3dReply>
What it's outputting to console:
Live: true Live: 225 Live: 1222
What it needs to output:
Live: true Viewers: 225 Duration: 1222
It needs to iterate through each node and do this, and I just can't figure it out. I tried using switch and while statements, but I just can't seem to get it to work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
而不是:
使用:
LocalName
属性为您提供节点的本地名称(isLive
、liveViewers
和liveDuration
)。如果需要,您可以对这些进行更多字符串操作。Instead of:
Use:
The
LocalName
property gives you the local name of the node (isLive
,liveViewers
andliveDuration
). You can do more string manipulation on these if needed.