我发现很多主题展示了解析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})