[英]“context instance” for a XElement linq to XML query using a where clause
I want to return a list of string values from an XElement collection and I get this error when constructing my code and don't see what I'm missing. 我想从XElement集合返回一个字符串值列表,并且在构造代码时收到此错误,但看不到我所缺少的内容。
Here's a section of the class I've written concering the problem: 这是我编写的属于该问题的课程的一部分:
private XElement _viewConfig;
public ViewConfiguration(XElement vconfig)
{
_viewConfig = vconfig;
}
public List<string> visibleSensors()
{
IEnumerable<string> sensors = (from el in _viewConfig
where el.Attribute("type").Value == "valueModule"
&& el.Element.Attribute("visible") = true
select el.Element.Attribute("name").Value);
return sensors.ToList<string>();
}
The XElement collection is of the form XElement集合的形式为
<module name="temperature" type="valueModule" visible="true"></module>
<module name="lightIntensity" type="valueModule" visible="true"></module>
<module name="batteryCharge" type="valueModule" visible="true"></module>
<module name="VsolarCells" type="valueModule" visible="false"></module>
First of all XElement
is not an IEnumerable
therefore the first line from el in _viewConfig
is not valid. 首先,
XElement
不是IEnumerable
因此from el in _viewConfig
的第一行无效。 If this is coming from a valid XML file, I presume the <module>
elements are contained inside a parent element (eg, <modules>
). 如果这来自有效的XML文件,则假定
<module>
元素包含在父元素(例如<modules>
)内。 If you make _viewConfig
to point to modules
then the following will work: 如果使
_viewConfig
指向modules
则将执行以下操作:
IEnumerable<string> sensors = (
from el in _viewConfig.Elements()
where el.Attribute("type").Value == "valueModule"
&& el.Attribute("visible").Value == "true"
select el.Attribute("name").Value);
Also note that type of el
is XElement
, therefore it doesn't have a property called Element
which I also removed from above (along with a few syntax errors that I had to fix: usage of ==
instead of =
for comparison, and using string literal "true"
instead of boolean true
to compare the value of an attribute textual value). 还要注意,
el
类型是XElement
,因此它没有称为Element
的属性,我也从上面删除了该属性(以及一些我必须修复的语法错误:使用==
代替=
进行比较,并使用字符串文字"true"
而不是布尔值true
以比较属性文字值的值)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.