Linq到XML查询..元素名称的问题



我试图查询xml文件的一组特定的值。下面是我要查询的xml文件的一部分。

<Troubleshooter guid="5f159d5d-4dec-4caf-81e5-645d77e05c84">
<Name>Battery</Name>
<AnalysisLog>
  <LogEntry guid="76e4b077-bb50-4000-9563-7f5aa0c9dc26">
    <Name>Battery Information</Name>
    <Severity>Informational</Severity>
    <Description></Description>
    <Details>
      <Detail guid="118bf18a-13d4-4226-b207-f2ae1638de8b">
        <Name>Battery ID</Name>
        <Value> 198311-85MO06047</Value>
      </Detail>
      <Detail guid="85b01a9b-bb18-4f71-8d12-6f7dec4b3705">
        <Name>Manufacturer</Name>
        <Value>11-85</Value>
      </Detail>
      <Detail guid="1b9d5465-63f4-4c5d-8259-93effc455084">
        <Name>Manufacture Date</Name>
        <Value></Value>
      </Detail>
      <Detail guid="2229029f-aa9e-4591-989a-32223a114538">
        <Name>Serial Number</Name>
        <Value> 1983</Value>
      </Detail>
      <Detail guid="24e6973f-f544-4a33-876d-359ebc56336e">
        <Name>Chemistry</Name>
        <Value>LION</Value>
      </Detail>
      <Detail guid="8676c4f7-8918-4007-af80-76e308ca983c">
        <Name>Long Term</Name>
        <Value>1</Value>
      </Detail>
      <Detail guid="8800d772-7da3-48a1-b1e4-fc86df0e49cf">
        <Name>Sealed</Name>
        <Value>0</Value>
      </Detail>
      <Detail guid="17d29a01-f010-4f66-bf60-c121e35cfc2b">
        <Name>Cycle Count</Name>
        <Value></Value>
      </Detail>
      <Detail guid="beb3f51a-9d89-42ad-81c4-5f9b7f682fa4">
        <Name>Design Capacity</Name>
        <Value>32249</Value>
      </Detail>
      <Detail guid="b42aa79e-8ee8-44ae-8a11-5fe87cf2822b">
        <Name>Last Full Charge</Name>
        <Value>32249</Value>
      </Detail>
    </Details>
  </LogEntry>
</AnalysisLog>

现在,我试图用来获取值的代码如下所示:

Imports System
Imports System.Xml
Imports System.Linq
Imports System.Xml.Linq
Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim datapath As String = "c:energyrep.xml"
        Dim xelement As XElement = xelement.Load(datapath)
        Dim dcValue = _
              From dc In xelement.Elements("detail") _
              Where CStr(dc.Element("name").Value) = "Design Capacity" _
              Select dc
        For Each xEle As XElement In dcValue
            MessageBox.Show(xEle.Element("value").Value)
        Next xEle
        Me.Close()
    End Sub
End Class

我认为问题出在details元素名称上。如果它有特定的指南。我认为,因为它与"/detail"关闭,你将能够以同样的方式查询标题。任何帮助将非常感激!谢谢!

编辑:澄清

我正在寻找嵌套在"detail"元素中的元素"value",以及具有值为"Design Capacity"的"name"元素。我需要它从

返回"32249"值:
<Detail guid="beb3f51a-9d89-42ad-81c4-5f9b7f682fa4">
 <Name>Design Capacity</Name>
 <Value>32249</Value>

XML区分大小写。如果您正在寻找Detail元素,那么您需要搜索Detail,而不是detail

    Dim dcValue = _
          From dc In xelement.Elements("Detail") _
          Where CStr(dc.Element("Name").Value) = "Design Capacity" _
          Select dc
    For Each xEle As XElement In dcValue
        MessageBox.Show(xEle.Element("Value").Value)
    Next xEle

Here We Go…(c#)

        var readSP = from a in xd.Descendants("Detail")
                     where a.Element("Name").Value == "Design Capacity"
                     select a.Element("Value").Value;

相关内容

  • 没有找到相关文章

最新更新