[英]VB.net parse XML nodes <value>
我發現很多主題都顯示了解析XML文件的不同方法,但是它並沒有實現我想要的功能。 確實,我需要解析多個節點才能獲取更多數據:
<data_results data_size="64">
<data>
<row>
<value>192.168.53.11|</value>
<value>22951.2138889</value>
</row>
<row>
<value>192.168.99.100|</value>
<value>22749.0361111</value>
</row>
</data>
</data_results>
目的是取回具有相應字節數/秒的每個IP地址,並將它們放在表的新行中。
但是,如何從XML文檔中獲取這些信息?
謝謝你的支持。
干杯。
您可以嘗試以下方法:
Dim xml As XDOcument = XDocument.Parse(xmlString)
Dim listRows As List<XElement> = xml.Descendants("row").ToList()
For Each node As XElement in listRows
Dim values As List<XElement> = node.Descendants("value").ToList()
// first value is the IP
Dim ip As String = values(0)
// second value is the bytes
Dim bytes As String = values(1)
Next
試圖將C#代碼轉換為VB,可能在那里出現一兩個語法錯誤。
我終於設法得到自己的解決方案:
' Look for the tag "row" into the XML doc
Dim items4 = doc4.GetElementsByTagName("row")
Dim items5 = doc4.GetElementsByTagName("value")
' Parse all the XML tag with "row" and get the value for each expected attributes
For Each item4 As System.Xml.XmlElement In items4
ounter1 = 0
For Each item5 As System.Xml.XmlElement In items5
' If it's the first value tag then it's the host IP address
If counter1 = 0 Then
' Get the host IP address
host = item4.ChildNodes(0).InnerText
Console.WriteLine("host : " & host)
' If it's the second value tag then it's the number of Bytes/s
ElseIf counter1 = 1 Then
' Get the host IP address
bytesPerSec = item4.ChildNodes(1).InnerText
Console.WriteLine("bytesPerSec : " & bytesPerSec)
' Otherwise it's an error
Else
' Do nothing
End If
' Increment counter
counter1 = counter1 + 1
Next
' Load the data into the target table
DataGridView10.Rows.Add(New String() {host, bytesPerSec})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.