I have an XML like this:
And I have a Member class with property Name.
How can I read every Unit and its children Units into multiple generic List
This would do it, using plain recursion:
public class Unit
{
public string Name { get; set; }
public List Children { get; set; }
}
class Program
{
public static void Main()
{
XDocument doc = XDocument.Load("test.xml");
List units = LoadUnits(doc.Descendants("Units").Elements("Unit"));
}
public static List LoadUnits(IEnumerable units)
{
return units.Select( x=> new Unit()
{ Name = x.Attribute("Name").Value,
Children = LoadUnits(x.Elements("Unit"))
}).ToList();
}
}