Your problem is that you are adding all groups to same instance of groups list. And then you are assigning that list with all groups inside to all menus.
Here is correct code:
var menus = new List<Menu>();
foreach (XmlNode menuNode in nodeList)
{
var groupName = new List<Group>(); // create list here
foreach (XmlNode childNode in menuNode)
groupName.Add(new Group { name = childNode.Attributes["name"].Value });
menus.Add(new Menu {
name = menuNode.Attributes["name"].Value,
group = groupName
});
}
You can use Linq to Xml:
var xdoc = XDocument.Load(file);
var menus = xdoc.Descendants("role")
.Where(r => (string)r.Attribute("name") == "admin")
.Elements("menu")
.Select(m => new Menu {
name = (string)m.Attribute("name"),
group = m.Elements("group")
.Select(g => new Group {
name = (string)g.Attribute("name")
}).ToList()
}).ToList();
Or with xpath:
var menus = xdoc.XPathSelectElements("//role[@name='admin']/menu")
.Select(m => new Menu { /* create menu as above */ })
.ToList();
BTW consider to use better naming and pascal case for public member names:
public class Menu
{
public string Name { get; set; }
public List<Group> Groups { get; set; }
}