How can I use Linq to create an array of dictionaries from XML?

Posted by Duke on Stack Overflow See other posts from Stack Overflow or by Duke
Published on 2010-04-26T14:41:11Z Indexed on 2010/04/26 14:43 UTC
Read the original article Hit count: 229

Filed under:
|
|
|
|

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("...xml 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]}

© Stack Overflow or respective owner

Related posts about c#

Related posts about Xml