XML odczytujący węzły potomne
Mam dokument XML i próbuję uzyskać childnodes elementu o nazwieUnit
Mój dokument XML jest tak ukształtowany:
<Unit>
<id>3</id>
<name>System Information</name>
<description>null</description>
... other ...
</Unit>
To jest kod, którego używam, aby spróbować je przeczytać.
public void Load()
{
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
XmlDocument xmldoc = new XmlDocument();
XmlNodeList xmlnode;
xmldoc.Load(fs);
xmlnode = xmldoc.GetElementsByTagName("Units");
for (int i = 0; i < xmlnode.Count; i++)
{
string str = string.Format("ID: {0}\r\nName:{0}", xmlnode[i].ChildNodes.Item(0).InnerText, xmlnode[i].ChildNodes.Item(1).InnerText);
MessageBox.Show(str);
}
}
Ale problem polega na tym, że kiedy próbuję je zdobyć, zamiast otrzymywać przedmiot 0 lub przedmiot 1, wyświetla wszystkie przedmioty, a nie przedmioty, które wybrałem.