我正在尝试从 xml 文件中读取数据并将其显示在文本框中,但它仅显示最后一个元素/属性,在本例中为“耐力”。这是我的xml文件
<?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>
我的读者代码如下
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();
}
}
因此,每当我单击按钮显示数据时,文本框中唯一显示的是 Endurance: 5