简体   繁体   中英

Linq to XML query… Issues with element name

I'm trying to query an xml file for a specific set of values. Below is part of the xml file I'm trying to query.

<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>

Now, the code I'm trying to use to get the value is as shown below:

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

I think the problem is with the details element name. I it has the specific guid. I would think since it closes with "/detail" that you would be able to query the header the same way. Any help would be greatly appreciated! Thanks!

EDIT: CLARIFYING

I'm looking for an element "value" nested in a "detail" element, along with a "name" element that has a value of "Design Capacity". I need it to return the "32249" value from:

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

XML is case-sensitive. If you are looking for Detail elements, then you need to search for Detail , not 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM