Whats wrong with my code?

XmlTextReader textReader = new XmlTextReader(@"D:\xml_file.xml");

        // If the node has value

        while (textReader.Read())
            // Move to fist element

            Console.WriteLine("XmlTextReader Properties Test");
            // Read this element's properties and display them on console
            Console.WriteLine("id:" + textReader.id.ToString());
            Console.WriteLine("name:" + textReader.name.ToString());
            Console.WriteLine("time:" + textReader.time.ToString());

show erron on: id, name, time

My XML file:

<name>Rikko Nora</name>
<name>Rikko Nora2</name>

A few things.

1) Look at the compilation errors given to you. You should have several messages telling you that "id", "name", and "time" are not properties of the XmlTextReader class. Work to try to fix your mistakes.
2) Since .NET 2.0, explicit usage of the XmlTextReader class has been discouraged in favor of using the XmlReader class and the static .Create method to obtain an instance of it.
3) Another option for you, and one that sort of falls more in line with how you are attempting to work, is to use LINQ to read and parse your XML file. Look at LINQ-to-XML (System.Xml.Linq) and using anonymous types to load your XML file and iterate over the contents.

Edited 6 Years Ago by apegram: n/a

This article has been dead for over six months. Start a new discussion instead.