我正在尝试配对两个元素的InnerText,我终于得到了正确的输出,但我不确定它是否有效。我正在考虑“从j ..”和“从k ...”它必须迭代两次元素?
是否有一个与我的解决方案相当的小组?
var query = from i in xdoc.Element("datasource")
.Elements("dataobject")
where (string)i.Attribute("type") == "HMIPage.Generic"
from j in i.Elements("property")
where (string)j.Attribute("name") == "PointRefParamName"
let param = j.Value
from k in i.Elements("property")
where (string)k.Attribute("name") == "PointRefPointName"
let point = k.Value
select new
{
PointName = point,
ParamName = param
};
foreach (var tag in query)
{
Console.WriteLine("{0}.{1}", tag.PointName, tag.ParamName);
}
这是源文件:
<datasource version="2">
...
<dataobject id="1" type="HMIPage.Generic" format="propertybag">
<property name="AddressFlags">0</property>
<property name="AddressType">0</property>
<property name="ObjectType">0</property>
<property name="PointRefFlags">0</property>
<property name="PointRefParamName">ModeState</property>
<property name="PointRefParamOffset">0</property>
<property name="PointRefPointName">01VE05_KMD</property>
<property name="PresentationType">0</property>
<property name="SecurityLevel">2</property>
<property name="UpdatePeriod">0</property>
<property name="version">1.2</property>
</dataobject>
<dataobject id="2" type="HMIPage.Generic" format="propertybag">
<property name="AddressFlags">1</property>
<property name="AddressType">0</property>
<property name="ObjectType">0</property>
<property name="PointRefFlags">0</property>
<property name="PointRefParamName">PointState</property>
<property name="PointRefParamOffset">0</property>
<property name="PointRefPointName">01VE05_P1</property>
<property name="PresentationType">0</property>
<property name="SecurityLevel">2</property>
<property name="UpdatePeriod">0</property>
<property name="version">1.2</property>
</dataobject>
<dataobject id="3" type="HMIPage.PushButton" format="propertybag">
<property name="Flags">0</property>
<property name="PBActionType">1</property>
<property name="RepeatFlag">0</property>
<property name="SecurityLevel">2</property>
<property name="StationIndex">0</property>
<property name="TaskLrn">21</property>
<property name="TaskParam1">1</property>
<property name="TaskParam2">223</property>
<property name="TaskParam3">0</property>
<property name="TaskParam4">0</property>
<property name="version">1.2</property>
</dataobject>
...
</datasource>
和输出:
01VE05_KMD.ModeState
01VE05_P1.PointState
答案 0 :(得分:1)
var query = xdoc.Root.Elements("dataobject")
.Where(x=>x.Attribute("type").Value=="HMIPage.Generic")
.Elements("property")
.Where(x=>x.Attribute("name").Value== "PointRefParamName"||x.Attribute("name").Value== "PointRefPointName")
.Select(x=>x.Value);