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
Why don't you implement a Tree for storing units. That would be much easier and natural than Lists.
Have a look at this comment for a good implementation using LinkedList.
See
If you HAVE to use List, then you may use recursion to build it. I'm assuming your Unit has a property (IList Unit.ChildUnits) to hold all children List. If not you may want to wrap Unit into another class that has this.
public List LoadAllUnits(XMLNode rootNode){
List allUnits = new List();
foreach(var childNode in rootNode.ChildNodes){
allUnits.Add(LoadAllSubUnits(childNode);
}
return allUnits;
}
private Unit LoadAllSubUnits(XMLNode node){
Unit u = GetUnitFromCurrentNode(node); // Converts current node into Unit object
if(root.HasChildNode){
foreach(var childNode in node.ChildNodes){
u.ChildUnits.Add(LoadAllSubUnits(childNode);
}
}
return u;
}