I am trying to parse a XML file in C# using Visual Studio and show the data in a ListBox, but I don\'t know how to parse it when I\'m dealing with a nested XML file.
You can get LikedPerson node and get it's name/age like you do now. In order to avoid code duplication, you can create a method, which takes XmlNode, parses it recursively and returns a Person. But the better way is to use XmlSerializer
foreach (XmlNode node in doc.DocumentElement)
{
string name = node.Attributes[0].Value;
int age = int.Parse(node["Age"].InnerText);
bool isMale = bool.Parse(node["IsMale"].InnerText);
var likedPerson = node.SelectSingleNode("LikedPerson");
if (likedPerson != null){
string name = likedPerson.Attributes[0].Value;
//age, gender, etc.
}
}