Linq to XML:查找特定元素的值
我是“Linq”和“Linq to XML”概念的新手。我有以下 xml 树,
<WLANProfile xmlns="http://www.microsoft.com/networking/WLAN/profile/v1">
<name>IWS</name>
<SSIDConfig>
<SSID>
<hex>496153</hex>
<name>ISL</name>
</SSID>
</SSIDConfig>
<connectionType>ESS</connectionType>
<MSM>
<security>
<authEncryption>
<authentication>WPA2PSK</authentication>
<encryption>AES</encryption>
<useOneX>false</useOneX>
</authEncryption>
<sharedKey>
<keyType>networkKey</keyType>
<protected>false</protected>
<keyMaterial>BFEBBEA9B0E78ECD671A8D35D96556A32E001B7524A1</keyMaterial>
</sharedKey>
</security>
</MSM>
</WLANProfile>
我想知道如何使用 linq to xml 检索 KeyMaterial 元素值?
我尝试使用以下代码,但我得到空枚举
var networkKey = from c in doc.Descendants("WLANProfile")
select (string)c.Element("keyMaterial").Value;
有什么建议吗?
I am new bee to "Linq" and "Linq to XML" concepts. I have the following xml tree
<WLANProfile xmlns="http://www.microsoft.com/networking/WLAN/profile/v1">
<name>IWS</name>
<SSIDConfig>
<SSID>
<hex>496153</hex>
<name>ISL</name>
</SSID>
</SSIDConfig>
<connectionType>ESS</connectionType>
<MSM>
<security>
<authEncryption>
<authentication>WPA2PSK</authentication>
<encryption>AES</encryption>
<useOneX>false</useOneX>
</authEncryption>
<sharedKey>
<keyType>networkKey</keyType>
<protected>false</protected>
<keyMaterial>BFEBBEA9B0E78ECD671A8D35D96556A32E001B7524A1</keyMaterial>
</sharedKey>
</security>
</MSM>
</WLANProfile>
I was wondering how to retrieve the KeyMaterial element value using linq to xml?
I have tried to use the following code, but I get empty enumeration
var networkKey = from c in doc.Descendants("WLANProfile")
select (string)c.Element("keyMaterial").Value;
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
两个错误:
1.)
keyMaterial
不是WLANProfile
的直接子级,这就是为什么您没有得到任何结果(c.Elements 只会查找直接子级)2 .) 您需要在 XML 中使用指定的命名空间 - 否则没有节点会匹配
两者都应用:
如果您知道总是只有一个键,则点表示法会稍微短一些:
Two mistakes:
1.)
keyMaterial
is not a direct child ofWLANProfile
that's why you don't get any results (c.Elements will only look for a direct child)2.) you need to use the specified namespace in the XML - otherwise no node will match
Both applied:
Somewhat shorter in dot notation if you know there is always going to be exactly one key: