【问题标题】:How to get an element within an attribute using LINQ如何使用 LINQ 获取属性中的元素
【发布时间】:2020-06-18 14:19:00
【问题描述】:

我正在尝试获取一个元素的值,该元素在 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。

【问题讨论】:

  • 你需要决定你想要的结果;你想要一个对象,例如{ CallInitials = "DW", Excitation = "15 V" },还是想要一个分隔字符串,比如“DW|15 W”,还是别的什么?

标签: c# xml linq linq-to-xml


【解决方案1】:

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

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

请注意,w =&gt; w.Element("Amplifier").Valuew.Element("Amplifier").Element("Excitation").Value 填充 15 V
w =&gt; w.Element("Amplifier") 填充&lt;Amplifier&gt;&lt;Excitation&gt;15 V&lt;/Excitation &lt;/Amplifier

如果你想要DW15 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();

希望对您有所帮助。

【讨论】:

  • @ravens23,这不是同一个 XML :) 我会检查并为更新添加答案。但如果你创建一个新问题将是最重要的。
  • 是的,XML 不同但概念相同。不想再问基本相同的问题。谢谢!
  • 谢谢!我很感激!
  • 大多数时候我有不止一个槽来存放所有数据。对于MeasuredValueDualCore,它不断返回null。你知道为什么吗?有时我会从索引 0 跳到 3 或类似的东西
  • @ravens23:分享整个 xml 和返回空值的代码,我会检查它。
猜你喜欢
  • 2021-05-29
  • 1970-01-01
  • 1970-01-01
  • 2021-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-23
  • 1970-01-01
相关资源
最近更新 更多