如何使用 LINQ 获取属性中的元素
How to get an element within an attribute using 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 returns 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
和 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();
希望对您有所帮助。
我正在尝试获取一个元素的值,该元素位于 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 returns 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
和 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();
希望对您有所帮助。