Convert Linq to XML -Dictionary

How to save next nodes in a dictionary, where int is the auto-generated key and string (node ​​value) using LINQ?

Elements:

XElement instructors =
         XElement.Parse(
                          @"<instructors>
                               <instructor>Daniel</instructor>
                               <instructor>Joel</instructor>
                               <instructor>Eric</instructor>
                               <instructor>Scott</instructor>
                               <instructor>Joehan</instructor> 
                         </instructors>"
        );

partially attempted code is given below :

var  qry = from instr in instructors.Elements("instructor")
where((p,index)=> **incomplete**).select..**incomplete**; 

How to include my choice in Dictionary<int,String>? (The key must begin with 1, in Linq pointers begin with zero).

+5
source share
1 answer

What about:

var dictionary = instructors.Elements("instructor")
                            .Select((element, index) => new { element, index })
                            .ToDictionary(x => x.index + 1,
                                          x => x.element.Value);
+8
source

All Articles