Taking 2 attributes from XElement to create a dictionary LINQ

Posted by AndyC on Stack Overflow See other posts from Stack Overflow or by AndyC
Published on 2010-06-09T16:54:58Z Indexed on 2010/06/09 17:02 UTC
Read the original article Hit count: 170

Filed under:
|

I'm trying to take one attribute to use as the key, and another to use as the value. If I use (xDoc is an XDocument object in the example):

Dictionary<string, XElement> test = xDoc.Descendants()
    .Where<XElement>(t => t.Name == "someelement")
    .ToDictionary<XElement, string>(t => t.Attribute("myattr").Value.ToString());

I get a dictionary with the myattr value as key (which is what I want) but the entire XElement object as the value.

What I want to do, is to select a second attribute to set as the value property on each dictionary item, but can't seem to figure that out.

Is it possible to do all of this in 1 Linq statement? Curiousity has caught me!

Cheers!

© Stack Overflow or respective owner

Related posts about c#

Related posts about LINQ