views:

84

answers:

3

My xml looks like:

<root>
  <blah1>some text</blah1>
  <someother>blah aasdf</someother>
</root>

I want to convert this to a dictionary

So I can do:

myDict["blah1"]

and it returns the text 'some text'

So far I have:

Dictionary<string,string> myDict = (from elem in myXmlDoc.Element("Root").Elements()
                                select elem.Value).ToDictionary<string,string>();

Is that correct or do I have to change the select to something with 2 results?

+1  A: 

you need a lambda in the ToDictionary call so it knows what to use for the key and what to use for the value...

check here for a good example, and here as well

Muad'Dib
+2  A: 

Specify what you want for Key and what for Value.

var myDict = myXmlDoc.Elements()
                     .ToDictionary( key => key.Name, val => val.Value);
Stan R.
+1  A: 
myXmlDoc.Root
    .Elements()
    .ToDictionary(xe => xe.Name, xe => xe.Value);
Lee