How can I use Linq to create an array of dictionaries from XML?
Given the following XML structure:
<courses>
<course>
<title>foo</title>
<description>bar</description>
</course>
...
</courses>
How could I create an array of dictionaries such that each dictionary contains all the element/value pairs within a course?
What I have right now generates an array whose elements contain a single key/value dictionary for each element/value pair in a course:
XElement x = XElement.Parse("...x开发者_JAVA百科ml string...");
var foo = (from n in x.Elements() select n)
.Elements().ToDictionary(y => y.Name, y => y.Value);
Produces:
[0] => {[course, foo]}
[1] => {[description, bar]}
What I'd like is this:
[0] => {[course, foo], [description, bar]}
Like this:
x.Elements("course")
.Select(c => c.Elements().ToDictionary(y => y.Name, y => y.Value))
.ToArray();
精彩评论