How to read key value from plist (xml) in C# -
i want string softwareversionbundleid & bundle version keys how can store dictionary can able easily?
<?xml version="1.0" encoding="utf-8"?> <!doctype plist public "-//apple//dtd plist 1.0//en" "http://www.apple.com/dtds/propertylist-1.0.dtd"> <plist version="1.0"> <dict> <key>genre</key> <string>application</string> <key>bundleversion</key> <string>2.0.1</string> <key>itemname</key> <string>appname</string> <key>kind</key> <string>software</string> <key>playlistname</key> <string>appname</string> <key>softwareiconneedsshine</key> <true/> <key>softwareversionbundleid</key> <string>com.company.appname</string> </dict> </plist>
i tried following code.
xdocument docs = xdocument.load(newfilepath); var elements = docs.descendants("dict"); dictionary<string, string> keyvalues = new dictionary<string, string>(); foreach(var in elements) { string key= a.attribute("key").value.tostring(); string value=a.attribute("string").value.tostring(); keyvalues.add(key,value); }
it throwing object reference exception.
<key>
along <string>
or <true/>
aren't attributes, child elements of <dict>
paired proximity. build dictionary, need zip them together, 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.value.value);
and result dictionary containing:
{ "genre": "application", "bundleversion": "2.0.1", "itemname": "appname", "kind": "software", "playlistname": "appname", "softwareiconneedsshine": "", "softwareversionbundleid": "com.company.appname" }
Comments
Post a Comment