.Net > Intermediate

What would be the output of the following program using XmlReader?

xml.xml file
<?xml version="1.0" encoding="utf-8" ?>
<root>
  <record>
    <name>Sumit</name>
    <age>30</age>
  </record>
  <record>
    <name>Manoj</name>
    <age>29</age>
  </record>
</root>




using System;
using System.Xml;
using System.Xml.Linq;


class Program
{
    static void Main()
    {
        using (XmlReader reader = XmlReader.Create("../../xml.xml"))
        {
            //reads the next node from the stream
            while (reader.Read())
            {
                //checks if the node is the start node, not the closing node
                if (reader.IsStartElement())
                {
                    //print the name of the node
                    if (reader.Name == "name" || reader.Name == "age")
                    {
                        Console.Write(reader.Name);
                        Console.Write(" ");
                        reader.Read();
                        Console.Write(reader.Value);
                        Console.WriteLine(" ");
                    }
                }
            }
        }
        Console.ReadLine();
    }
}
Answers :
Time Left
Question Number : /20
Please do not press back or refresh button during this test.

Login

Register

Next Question