[英]Select XElement where child element has a value
Given the following XML: 给出以下XML:
<platforms>
<platform>
<id>1</id>
<price>2.99</price>
</platform>
</platforms>
How can I select the "platform" element as an XElement object based on the child element "id" having a value of "1"? 如何根据值为“1”的子元素“id”选择“platform”元素作为XElement对象?
I have got this far: 我到目前为止:
XDocument xPlatformXml = new XDocument();
XElement xel = xPlatformXml.Element("platforms").Elements("platform").Where(x => x.Value == "1").SingleOrDefault();
But this is looking for the value to be in "platform" element rather than "id". 但这是在“平台”元素而不是“id”中寻找价值。
XDocument xPlatformXml = new XDocument();
XElement xel = xPlatformXml.Element("platforms")
.Elements("platform")
.Where(x => x.Element("id").Value == "1")
.SingleOrDefault();
Or using XElement
to int
conversion: 或者使用XElement
进行int
转换:
XDocument xPlatformXml = new XDocument();
XElement xel = xPlatformXml.Element("platforms")
.Elements("platform")
.Where(x => (int)x.Element("id") == 1)
.SingleOrDefault();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.