How to read key value from plist (xml) in C#

<key> along with <string> or <true/> aren't attributes, they are child elements of <dict> that are paired by proximity. To build your dictionary, you need to zip them together, like so:

        var keyValues = docs.Descendants("dict")
            .SelectMany(d => d.Elements("key").Zip(d.Elements().Where(e => e.Name != "key"), (k, v) => new { Key = k, Value = v }))
            .ToDictionary(i => i.Key.Value, i => i.Value.Value);

And the result is a dictionary containing:

{
  "genre": "Application",
  "bundleVersion": "2.0.1",
  "itemName": "AppName",
  "kind": "software",
  "playlistName": "AppName",
  "softwareIconNeedsShine": "",
  "softwareVersionBundleId": "com.company.appname"
}

There is an error in

a.Attribute("key").Value

Cause there is no attribute. You should use Name and Value property instead of attribute

More details you can check: XMLElement

foreach(var a in elements)
{
    var key= a.Name;
    var value = a.Value;
    keyValues.Add(key,value); 
}

There is another way for this approach

var keyValues = elements.ToDictionary(elm => elm.Name, elm => elm.Value);

Tags:

C#

Xml

Dictionary