Reputation: 155
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.
Upvotes: 1
Views: 3985
Reputation: 498972
Instead of:
Console.WriteLine("Live: " + reader.Value);
Use:
Console.WriteLine(string.Format("{0}: {1}", reader.LocalName, reader.Value));
The LocalName
property gives you the local name of the node (isLive
, liveViewers
and liveDuration
). You can do more string manipulation on these if needed.
Upvotes: 3