1

I have an xml document like below:

<?xml version = "1.0"?>
<Employees>
    <Employee Id="1" City="Seattle">
        <EmployeeName>Nancy Davolio</EmployeeName>
        <Country>USA</Country>
    </Employee>
    <Employee Id="2" City="Tacoma">
        <EmployeeName>Andrew Fuller</EmployeeName>
        <Country>USA</Country>
    </Employee>
    <Employee Id="3" City="Kirkland">
        <EmployeeName>Janet Leverling</EmployeeName>
        <Country>USA</Country>
    </Employee>
</Employees>

From this file, I like to get displayed in the console only the EmployeeName for every employee with their tags. Below is how I tried to do so but it is showing only the opening and closing tag but not the value of EmployeeName.

static void Main(string[] args)
        {


            XmlTextReader reader = new XmlTextReader("D://test.xml");
            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                    case XmlNodeType.Element: // The node is an element.
                        if (reader.Name == "EmployeeName")
                        {
                            Console.Write("<" + reader.Name);
                            Console.WriteLine(">");

                        }
                         break;
                    case XmlNodeType.Text: //Display the text in each element.
                         if (reader.Name == "EmployeeName")
                         {
                             Console.WriteLine(reader.Value);
                         }

                        break;
                    case XmlNodeType.EndElement: //Display the end of the element.
                        if (reader.Name == "EmployeeName")
                        {
                            Console.Write("</" + reader.Name);
                            Console.WriteLine(">");
                        }

                        break;
                }
            }
            Console.ReadLine();



        }

Any help, please!

Arbenita Musliu
  • 95
  • 4
  • 11

0 Answers0