2016-05-29 8 views
0

Ich habe eine folgende Details enthalten XML-Datei.Finden Sie Attribut Wert in XML-Datei VB.net

<?xml version="1.0" encoding="utf-8"?> 
<LoadSurveyValues> 
    <LoadSurvey timeStamp="18/12/2015 11:13:02" saveMode="SnapShot"> 
    <Date Date="18/12/2015 00:00:00"> 
     <ParamValues Type="ActiveTotalImport" Unit="kWh"> 
     <SipValues Time="00:15" ParamValue="0" /> 
     <SipValues Time="00:30" ParamValue="0" /> 
     <SipValues Time="00:45" ParamValue="0" /> 
     <SipValues Time="01:00" ParamValue="0" /> 
     <SipValues Time="01:15" ParamValue="0.5" /> 
     <SipValues Time="01:30" ParamValue="0.1" /> 
     <SipValues Time="01:45" ParamValue="0" /> 
     <SipValues Time="02:00" ParamValue="0" /> 
     <SipValues Time="02:15" ParamValue="0" /> 
     <SipValues Time="02:30" ParamValue="0" /> 

und so weiter

Ich möchte Attributwert, um herauszufinden, "ParamValue" in "Datum" Datum 18/12/2015 "ParamValues" Typ ActiveTotalImport“und "SipValue" Zeit 01:15 Uhr. das Ergebnis sollte 0,5 sein.

Dies sind Codes ich in VB.net mit Listboxen versucht.

Dim xr As XmlReader = XmlReader.Create("meter 01.xml") 
    If ListBox1.SelectedIndex >= 0 Then 
     If ListBox2.SelectedIndex >= 0 Then 
      If ListBox3.SelectedIndex >= 0 Then 

       If xr.NodeType = XmlNodeType.Element And xr = ListBox1.SelectedIndex And xr = ListBox2.SelectedIndex And xr = ListBox3.SelectedIndex Then 

        ListBox4.Items.Add(xr.GetAttribute(1)) 

       End If 
      End If 
     End If 
    End If 

ListBox1 18/12/2015 00.00.00 Wert ausgewählt hat, hat ListBox2 ausgewählt ActiveTotalImport Wert und ListBox3 hat 01:15 Wert ausgewählt. Daher fügt das Ergebnis der ListBox4 hinzu, die 0,5 ist. Aber dieser Code funktioniert nicht mir bitte helfen, es zu lösen out

Antwort

0

Try xml linq

Imports System.Xml 
Imports System.Xml.Linq 
Module Module1 
    Const FILENAME As String = "c:\temp\test.xml" 
    Sub Main() 
     Dim doc As XDocument = XDocument.Load(FILENAME) 

     Dim survey As XElement = doc.Descendants("LoadSurvey").Where(Function(x) x.Attribute("timeStamp").Value = "18/12/2015 11:13:02").FirstOrDefault() 
     Dim ParamValue As String = survey.Descendants("SipValues").Where(Function(x) x.Attribute("Time").Value = "01:15").Select(Function(y) y.Attribute("ParamValue")).FirstOrDefault() 
    End Sub 

End Module 
+0

dankt Ihnen sehr viel – user38701