Linq to XML query ...元素名称的问题

时间:2014-03-27 01:29:14

标签: .net xml vb.net linq linq-to-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

我认为问题在于细节元素名称。我有具体的指导。我认为,因为它以&#34; / detail&#34;您将能够以相同的方式查询标头。任何帮助将不胜感激!谢谢!

编辑:澄清

我正在寻找元素&#34;价值&#34;嵌套在&#34;细节&#34;元素,以及&#34;名称&#34;值为&#34;设计容量&#34;的元素。我需要它来返回&#34; 32249&#34;价值来自:

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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

我们去......(C#)

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