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

如何在C#中从plist(xml)读取键值,第1张

概述我只想获得softwareVersionBundleId&的字符串.捆绑版本密钥 我怎样才能将它存储到字典中以便我能够轻松搞定? <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0 我只想获得softwareversionBundleID&的字符串.捆绑版本密钥
我怎样才能将它存储到字典中以便我能够轻松搞定?
<?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>

我尝试了以下代码.

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);             }

它抛出对象引用异常.

解决方法 <键>以及< 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);

结果是一个字典包含:

06001

总结

以上是内存溢出为你收集整理的如何在C#中从plist(xml)读取键值全部内容,希望文章能够帮你解决如何在C#中从plist(xml)读取键值所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/langs/1262456.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存