Start New Discussion within our Software Development Community

Hi,
Whats wrong with my code?

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

        // If the node has value

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

            textReader.MoveToElement();
            Console.WriteLine("XmlTextReader Properties Test");
            Console.WriteLine("===================");
            // 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());
        }
        Console.ReadLine()

show erron on: id, name, time

My XML file:

<students>
<student>
<id>1</id>
<name>Rikko Nora</name>
<time>2010-03-12</time>
</student>
<student>
<id>2</id>
<name>Rikko Nora2</name>
<time>2010-05-15</time>
</student>
</students>

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.

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