C# &引用;“上下文实例”;对于使用where子句的XElement linq到XML查询
我想从XElement集合返回一个字符串值列表,但在构建代码时出现了这个错误,我看不出缺少了什么。 下面是我写的关于这个问题的一节课:C# &引用;“上下文实例”;对于使用where子句的XElement linq到XML查询,c#,linq-to-xml,xelement,C#,Linq To Xml,Xelement,我想从XElement集合返回一个字符串值列表,但在构建代码时出现了这个错误,我看不出缺少了什么。 下面是我写的关于这个问题的一节课: private XElement _viewConfig; public ViewConfiguration(XElement vconfig) { _viewConfig = vconfig; } public List<string> visibleSensors() { IEnumerable<string>
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>();
}
private-XElement\u-viewConfig;
公共视图配置(XElement vconfig)
{
_viewConfig=vconfig;
}
公共列表visibleSensors()
{
IEnumerable sensors=(来自视图配置中的el)
其中el.Attribute(“type”).Value==“valueModule”
&&el.Element.Attribute(“可见”)=真
选择el.Element.Attribute(“name”).Value);
返回传感器。ToList();
}
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>
首先
XElement
不是一个IEnumerable
,因此_viewConfig中el的第一行无效。如果这是来自有效的XML文件,我假定
元素包含在父元素中(例如
)。如果使\u viewConfig
指向模块
,则以下操作将起作用:
IEnumerable<string> sensors = (
from el in _viewConfig.Elements()
where el.Attribute("type").Value == "valueModule"
&& el.Attribute("visible").Value == "true"
select el.Attribute("name").Value);
IEnumerable传感器=(
来自_viewConfig.Elements()中的el
其中el.Attribute(“type”).Value==“valueModule”
&&el.属性(“可见”)。值==“真”
选择el.属性(“名称”).值);
还要注意的是,el
的类型是XElement
,因此它没有一个名为Element
的属性,我也从上面删除了该属性(还有一些语法错误,我必须修复:使用=
代替=
进行比较,并使用字符串文字“true”
而不是布尔值true
来比较属性文本值的值)。首先XElement
不是IEnumerable
因此,视图配置中el的第一行无效。如果这是来自有效的XML文件,我假定
元素包含在父元素中(例如
)。如果使\u viewConfig
指向模块
,则以下操作将起作用:
IEnumerable<string> sensors = (
from el in _viewConfig.Elements()
where el.Attribute("type").Value == "valueModule"
&& el.Attribute("visible").Value == "true"
select el.Attribute("name").Value);
IEnumerable传感器=(
来自_viewConfig.Elements()中的el
其中el.Attribute(“type”).Value==“valueModule”
&&el.属性(“可见”)。值==“真”
选择el.属性(“名称”).值);
还要注意的是,el
的类型是XElement
,因此它没有一个名为Element
的属性,我也从上面删除了该属性(还有一些语法错误,我必须修复:使用=
代替=
进行比较,并使用字符串文字“true”
而不是布尔值true
来比较属性文本值的值)。谢谢。我将开始合并并完成充实系统。谢谢。我将开始合并并完成系统的充实。