小编Man*_*han的帖子

如何在C#中从plist(xml)读取键值

我只想获取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)

它抛出对象引用异常.

c# xml dictionary

6
推荐指数
1
解决办法
1380
查看次数

标签 统计

c# ×1

dictionary ×1

xml ×1