我只想获取softwareVersionBundleId和bundle版本密钥的字符串如何将其存储到字典中以便我能够轻松获得?
<?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>
Run Code Online (Sandbox Code Playgroud)
我尝试了以下代码.
XDocument docs = XDocument.Load(newFilePath);
var elements = docs.Descendants("dict");
Dictionary<string, string> keyValues = new Dictionary<string, string>();
foreach(var a in elements)
{
string key= a.Attribute("key").Value.ToString();
string value=a.Attribute("string").Value.ToString();
keyValues.Add(key,value);
}
Run Code Online (Sandbox Code Playgroud)
它抛出对象引用异常.
<key>
与属性一起<string>
或<true/>
不属性,它们是<dict>
由邻近性配对的子元素.要构建字典,您需要将它们压缩在一起,如下所示:
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);
Run Code Online (Sandbox Code Playgroud)
结果是一个字典包含:
Run Code Online (Sandbox Code Playgroud){ "genre": "Application", "bundleVersion": "2.0.1", "itemName": "AppName", "kind": "software", "playlistName": "AppName", "softwareIconNeedsShine": "", "softwareVersionBundleId": "com.company.appname" }
归档时间: |
|
查看次数: |
1380 次 |
最近记录: |