簡體   English   中英

在XML文件VB.net中查找屬性值

[英]Find Attribute Value in XML file VB.net

我有以下詳細信息包括xml文件。

<?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" />

等等

我想在“日期”日期18/12/2015,“ ParamValues”類型ActiveTotalImport”和“ SipValue”時間01:15中找出“ ParamValue”屬性值,結果應為0.5。

這些是我在VB.net中使用列表框嘗試過的代碼。

  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值,ListBox2選擇了ActiveTotalImport值,ListBox3選擇了01:15值。 因此,結果將添加到值為0.5的ListBox4中。 但是此代碼不起作用,請幫我解決

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM