0

I just recently posted about trying to get an XMLTextReader to work, and I finally did. Unfortunately, now I cannot get it to take the data from the XML file because I am doing something wrong with my if statements. I need to have these if statements inside another if statement because they need the Student class to be created first. I have no idea how to approach this anymore. Below is my code, I am sure it is something stupid. I am not good a coding so I know its something I am missing.

 while (reader.Read())
    {
        reader.MoveToContent();            

        if (reader.NodeType == XmlNodeType.Element && reader.Name == "student")
        {
            Student s = new Student();

                if (reader.Name == "id")
                {
                    s.ID = reader.ReadString();
                }
                if (reader.Name == "firstname")
                {
                    s.FirstName = reader.ReadString();
                }
                if (reader.Name == "lastname")
                {
                    s.LastName = reader.ReadString();
                }
                if (reader.Name == "score")
                {
                    s.TestScores.Add(Convert.ToInt32(reader.ReadString()));
                }
            s.Average = 6.00;
            Students.Add(s);
        }
    }

[EDIT] Even after I tell the XMLreader to move on to the next line with reader.MoveToCOntent(); it still skips all of the if statements.

I am begginers in programming, I would appreciate any new ideas and suggestions.

Jess
  • 121
  • 2
  • 2
  • 4
  • So what "isn't working", and *how* isn't it working? Unless the *real* issue can be summed up in the title, this ought to be closed. –  Apr 14 '12 at 09:15
  • 2
    The more relevant anwer on your other question: __do not use XmlReader__ (unless your data is much > 100 MB) – H H Apr 14 '12 at 09:22
  • Use debugger to check if your first if statement is valid and then proceed to the next. However, it seems strange that you are checking if reader.Name == "student" and then checking again if reader.Name == "something different". – wegelagerer Apr 14 '12 at 09:26

3 Answers3

2

In order for the first if statement to be true, reader.Name already has to be "student". Any nested if checks for the value of reader.Name to be anything other than "student" is always going to be false.

Tung
  • 5,334
  • 1
  • 34
  • 41
  • Thank you so much Tung for your quick response. The only problem is that even when I adding (reader.MoveToContent();) after creating the new Student, it still skips over all the if statements and just sets the Student Average and adds it to the list. Any suggestions? – Jess Apr 14 '12 at 09:25
  • `reader.MoveToContent()` will not move the reader forward if you are already at a content node so issuing another `reader.MoveToContent()` after your first `if` check will not do anything. If you want to move the reader forward while it is already at a content node, you will need to issue a `Read()`. Keep in mind that `Read()` might land you on a non element node (such as whitespace) so you need to combine it with `MoveToContent()` – Tung Apr 14 '12 at 09:46
  • 1
    I agree with the rest of the commentators that you should try a less complicated approach to parse the Xml. While the XmlReader is efficient, it may not be worth the time you will have to invest in order to get it right, especially since you are not parsing very large Xml. If you must use the XmlReader, then refer to the [documentation](http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx), as you will need to understand how each method affects the current position of the reader. – Tung Apr 14 '12 at 10:08
1

it still skips over all the if statements and just sets the Student Average and adds it to the list

The first thing I would think is that equality on string fails, so first thing to try is

 if (reader.Name.Equals("id",StringComparison.InvariantCultureIgnoreCase))..

if this does not work as well, try to use simple XmlDocument and see if it works, if not, I would say something else in your code is wrong, which is not visible form the code provided.

A simple sample of use of XmlDocument can find here:

Reading XML File using XmlDocument

Tigran
  • 61,654
  • 8
  • 86
  • 123
0

its not easy to say unless you upload some errors about why its not working. There is nothing wrong with syntax. I would say see debugging error in visual studio instead to get details. and if you don't understand error then upload it hear after that we can guide you.

i think you should try closing reader. your reader is opened. write reader.close() at the end.