我发现很多主题都显示了解析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文档中获取这些信息?
感谢您的支持。
干杯。
答案 0 :(得分:0)
你可以试试这个:
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,可能还有一两个语法错误。
答案 1 :(得分:0)
我终于设法得到了自己的解决方案:
' 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})