选择XElement,其中子元素具有值 [英] Select XElement where child element has a value
本文介绍了选择XElement,其中子元素具有值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给出以下XML:
<platforms>
<platform>
<id>1</id>
<price>2.99</price>
</platform>
</platforms>
如何基于值为"1"的子元素"id"选择"platform"元素作为XElement对象?
How can I select the "platform" element as an XElement object based on the child element "id" having a value of "1"?
我已经走了这么远:
XDocument xPlatformXml = new XDocument();
XElement xel = xPlatformXml.Element("platforms").Elements("platform").Where(x => x.Value == "1").SingleOrDefault();
但这是要在"platform"元素而不是"id"中查找值.
But this is looking for the value to be in "platform" element rather than "id".
推荐答案
XDocument xPlatformXml = new XDocument();
XElement xel = xPlatformXml.Element("platforms")
.Elements("platform")
.Where(x => x.Element("id").Value == "1")
.SingleOrDefault();
或使用XElement
到int
转换:
XDocument xPlatformXml = new XDocument();
XElement xel = xPlatformXml.Element("platforms")
.Elements("platform")
.Where(x => (int)x.Element("id") == 1)
.SingleOrDefault();
这篇关于选择XElement,其中子元素具有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文