c# - 如何使用 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。
解决方案
如果你想要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").Value
或w.Element("Amplifier").Element("Excitation").Value
填充15 V
和填充。w => w.Element("Amplifier")
<Amplifier><Excitation>15 V</Excitation </Amplifier
如果你想要DW
and 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();
我希望你觉得这有帮助。
推荐阅读
- sql - 如何在mybatis mapper的同一个select中提取一个pojo和一个动态图?
- c++ - stl 函数和函数类型 vs 函数指针类型
- amazon-web-services - 在 AWS SAM 模板中创建 RDS 完全访问角色
- javascript - 页面加载上的活动类未触发
- haskell - Haskell 避免在返回可能的函数中使用双重包装的可能
- c# - 如何在不按住鼠标左键的情况下用鼠标拖动任何对象?
- javascript - JavaScript - 复选框循环未正确汇总价格
- doctrine - Symfony4:当 OneToMany 和 OneToOne 在同一实体上时删除问题
- azure-pipelines - 托管代理 devops 需要很长时间或取消
- codeigniter - Codeigniter 3 应用程序错误:如果标题中有变音符号,将标题转换为 slug 不起作用