1

i have this xml:

<plist version="1.0">
<dict>
    <key>Johnson</key>
    <dict>
        <key>id</key>
        <string>4525434</string>
        <key>name</key>
        <string>Neil Johnson</string>
        <key>firstname</key>
        <string>neil</string>
        </dict>
    <key>Adam</key>
    <dict>
        <key>id</key>
        <string>481689</string>
        <key>name</key>
        <string>Andrew Adam</string>
        </dict>
</dict>

How to parse this and display the data based on key Johnson and Adam

I tried this:

var xmlDict = doc.Root.Element("dict");
Dictionary<string, string> dict = xmlDict.Elements("key")
           .Zip(xmlDict.Elements("string"), (k, s) => new KeyValuePair<string, string>(k.Value, s.Value))
           .ToDictionary(x => x.Key, x => x.Value);

But this will only add the items to inner Dictionary.

MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
user2056563
  • 600
  • 2
  • 12
  • 37

1 Answers1

3

Your data is nested, so you need Dictionary<string, Dictionary<string, string>> to store it. Following query should do the trick:

var dict = xmlDict.Elements("key")
           .Zip(xmlDict.Elements("dict"), (k, v) => new { k, v })
           .ToDictionary(
                x => (string)x.k,
                x => x.v.Elements("key")
                        .Zip(x.v.Elements("string"), (k, v) => new { k, v })
                        .ToDictionary(y => (string)y.k, y => (string)y.v));
MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
  • Thanks, works fine, i have one more query if you can please help me http://stackoverflow.com/questions/22060464/overriding-methods-values-in-app-xaml-cs – user2056563 Feb 27 '14 at 08:55
  • Can you please help me on this http://stackoverflow.com/questions/23948975/parse-xml-with-same-key-values – user3114009 May 30 '14 at 07:13