我这里有个xml的
<?xml version="1.0" encoding="utf-8" ?>
<Group>
<Person>
<Name>John Smith</Name>
<Age>21</Age>
<Gender>male</Gender>
<Address>Road 1</Address>
</Person>
<Group>
<Person>
<Name>John see</Name>
<Age>23</Age>
<Gender>male</Gender>
<Address>Road 2</Address>
</Person>
</Group>
这个是我的xml文件
我用这个方法读取元素name age gender address
public void ReadXml()
{
XmlTextReader reader = new XmlTextReader(@"C:\XMLFile1.xml");
while(reader.Read())
{
if(reader.NodeType == XmlNodeType.Element && reader.Name =="Person")
{
while(!(reader.NodeType == XmlNodeType.EndElement && reader.Name == "Person"))
{
string name;
int age;
string gender;
string address;
if(reader.NodeType == XmlNodeType.Element)
{
switch(reader.Name)
{
case"Name":
name = reader.ReadString();
break;
case"Age":
age = Int32.Parse(reader.ReadString());
break;
case"Gender":
gender = reader.ReadString();
break;
case"Address":
address = reader.ReadString();
break;
default:
break;
}
}
MessageBox.Show(name+""+Convert.ToInt32(age)+""+gender+""+address);
}
}
}
}