1

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.

user1104783
  • 155
  • 2
  • 11
  • 1
    Out of interest, *why* is anything other than XmlReader not an option? When giving limitations, it's always useful to provide reasons, as they can affect the answers. – Jon Skeet Dec 25 '11 at 18:18
  • 1
    Also, don't use `new XmlTextReader()`. Use `XmlReader.Create()`. – John Saunders Dec 25 '11 at 18:31
  • I guess it is nice to dispose it so use using : using (var xtr = XmlReader.Create(uri)) – Iman Dec 29 '11 at 16:36

1 Answers1

3

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.

Oded
  • 489,969
  • 99
  • 883
  • 1,009