0

我正在尝试将两个元素的 InnerText 配对,最终得到了正确的输出,但我不确定它是否有效。我在想“from j..”和“from 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

4

1 回答 1

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);
于 2013-08-15T21:11:30.713 回答