首页 > 解决方案 > 如何使用 LINQ 获取属性中的元素

问题描述

我正在尝试获取位于 2 个属性下然后在另一个元素下的元素的值。

<Setup>
        <Devices>
            <SampleRate>20000</SampleRate>
            <Device Type="AI">
                <Slot Index="0">
                   <OutputChannel>
                    <MeasuredQuantity>VOLTAGE</MeasuredQuantity>
                    <DualCore>True</DualCore>
                   </OutputChannel
                </Slot>
                <Slot Index="1">
                </Slot>
                <Slot Index="2">
                    <OutputChannel>
                    <MeasuredQuantity>VOLTAGE</MeasuredQuantity>
                    <DualCore>True</DualCore>
                   </OutputChannel
                </Slot>
            </Device>
        </Devices>
</Setup>

我将如何获得测量值?

string typeGage = setupFile.Root.Descendants("DewesoftSetup").Descendants("Devices")
                                .Where(w => w.Element("Device").Attribute("Type").Value == "AI")
                                .Where(w => w.Element("Slot").Attribute("Index").Value == i.ToString())
                                .Select(w => w.Element("MeasuredValue").Value)
                                .Single();

这会引发异常(我猜我不能使用“Where”两次)

更新

      for (int i = 0; i < Form1.app.Data.AllChannels.Count; i++)
      {
         found = false;
         IChannel ch = Form1.app.Data.AllChannels[i];

         if (ch.Used == true)
         {
            for (int row = 24; row <= 43; row++)
            {
               if (oSheetLocal.Cells[row, 2].Value == null)
               {
                  string typeGage = setupFile.Root.Descendants("Setup").Descendants("Devices")
                                   .Where(w => w.Element("Device").Attribute("Type").Value == "AI" &&
                                          w.Element("Device").Element("Slot").Attribute("Index").Value == i.ToString())
                                   .Select(w => w.Element("Device").Element("Slot").Element("OutputChannel").Element("MeasuredQuantity").Value)
                                   .FirstOrDefault();
}}}}

它在第一次运行时效果很好,但我尝试让索引 2 返回 null。

标签: c#xmllinqlinq-to-xml

解决方案


如果你想要Excitation,你可以使用:

string excitation = sensorDoc.Root.Descendants("Sensor")
            .Where(w => w.Attribute("ID").Value == sensorSN)
            .Select(w => w.Element("Amplifier").Value)
            .FirstOrDefault();

请注意 ,w => w.Element("Amplifier").Valuew.Element("Amplifier").Element("Excitation").Value填充15 V和填充。
w => w.Element("Amplifier")<Amplifier><Excitation>15 V</Excitation </Amplifier

如果你想要DWand 15 V,你可以使用:

var calInitialsAndExcitation = sensorDoc.Root.Descendants("Sensor")
                .Where(w => w.Attribute("ID").Value == sensorSN)
                .Select(w => 
                new { 
                        CalInitials = w.Element("CalInitials").Value, 
                        Excitation = w.Element("Amplifier").Element("Excitation").Value
                    }).FirstOrDefault();

更新

string typeGage = setupFile.Root.Descendants("Setup").Descendants("Devices")
            .Where(w => w.Element("Device").Attribute("Type").Value == "AI" 
                && w.Element("Device").Element("Slot").Attribute("Index").Value == "0")
            .Select(w => w.Element("Device").Element("Slot").Element("MeasuredValue").Value)
            .FirstOrDefault();

根据评论更新

var typeGage = setupFile.Descendants("Device")
        .Where(d => d.Attribute("Type").Value == "AI")
        .Descendants("Slot")
        .Where(s => s.Attribute("Index").Value == "2")
        .Select(m => m.Element("OutputChannel")?.Element("MeasuredQuantity")?.Value)
        .FirstOrDefault();

我希望你觉得这有帮助。


推荐阅读