C# XmlTextReader not reading all elements and attributes

andys

New Member
I'm trying to read data from an xml file and display it in a text box, but it is only displaying the last element/attribute, in this case "Endurance". Here is my xml file\[code\]<?xml version="1.0" encoding="utf-8"?><Character> <Name Name="Test" /> <Age Age="19" /> <Class Class="Necromancer" /> <Strength Strength="1" /> <Dexterity Dexterity="2" /> <Intelligence Intelligence="3" /> <Speed Speed="4" /> <Endurance Endurance="5" /></Character>\[/code\]My code for the reader is as follows\[code\]XmlTextReader reader = new XmlTextReader(openFileDialog1.FileName);while (reader.Read()){ if (reader.HasAttributes) { for (int i = 0; i < reader.AttributeCount; i++) { reader.MoveToAttribute(i); switch (reader.Name) { case "Name": DisplayBox.Text = "Name: " + reader.Value + "\n"; break; case "Age": DisplayBox.Text = "Age: " + reader.Value + "\n"; break; case "Class": DisplayBox.Text = "Class: " + reader.Value + "\n"; break; case "Strength": DisplayBox.Text = "Strength: " + reader.Value + "\n"; break; case "Dexterity": DisplayBox.Text = "Dexterity: " + reader.Value + "\n"; break; case "Intelligence": DisplayBox.Text = "Intelligence: " + reader.Value + "\n"; break; case "Speed": DisplayBox.Text = "Speed: " + reader.Value + "\n"; break; case "Endurance": DisplayBox.Text = "Endurance: " + reader.Value + "\n"; break; default: break; } } reader.MoveToElement(); }}\[/code\]So whenever I click the button to display the data, the only thing that shows up in the text box is Endurance: 5
 
Back
Top