There is an xml file:

<?xml version="1.0" encoding="utf-8"> <Groups> <Group ID ="1121321" Master="He1by"> <Name>New name </Name> <User>UserName</User> <User>UserName2</User> </Group> </Groups> 

How to get User field values? If there is a following query:

 var items=(from xe in fdoc.Element("Groups")?.Elements("Group") where xe.Element("Name").Value.Equals(str) select xe); 

Ie, how can I enter, for example, in the List, the values ​​UserName and UserName2?

    1 answer 1

     var items=(from xe in fdoc.Element("Groups")?.Elements("Group") where xe.Element("Name").Value.Equals(str) select (from xel in xe.Elements("User") select xel.Value).ToList()); 

    However, such a query returns an IEnumerable<List<String>> for the reason that for each element of the Group type a list of User values ​​will be returned.

    If your Name filter should return one value (or nothing if it fails) add FirstOrDefault ()

     var items=(from xe in fdoc.Element("Groups")?.Elements("Group") where xe.Element("Name").Value.Equals(str) select (from xel in xe.Elements("User") select xel.Value).ToList()).FirstOrDefault(); 

    The result of such a query will be List<String>

    To check that the value of Name is unique, you can use the SingleOrDefault method, then an exception will be thrown if there are several elements with the required Name