Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
列表框中的XML数据_Xml_Vb.net - Fatal编程技术网

列表框中的XML数据

列表框中的XML数据,xml,vb.net,Xml,Vb.net,我有一个包含服务器数据的XML文件。服务器的国家/地区和IP <?xml version="1.0"?> <servers> <server> <location>Belgium</location> <ip>192.168.0.1</ip> </server> <server> <location>The Netherlands</locatio

我有一个包含服务器数据的XML文件。服务器的国家/地区和IP

<?xml version="1.0"?>
<servers>

<server>
   <location>Belgium</location>
   <ip>192.168.0.1</ip>
</server>

<server>
    <location>The Netherlands</location>
    <ip>127.0.0.6</ip>
</server>

</servers>

如何将IP和ping添加到列表框中的IP位置?

对于某些任务,
XmlReader
类很有用,但根据设计,它一次只检查一个节点。在这种情况下,您需要以随机访问方式同时访问多个元素,使用
XmlDocument
XDocument
类将更容易。例如:

Dim doc As New XmlDocument()
doc.Load("http://127.0.0.1/servers.xml")
For Each serverNode As XmlNode In doc.SelectNodes("/servers/server")
    Dim location As String = serverNode.SelectSingleNode("location").InnerText
    Dim ip As String = serverNode.SelectSingleNode("ip").InnerText
Next
lsbServers.Items.Add(location & " - " & ip)) 
或者

或:


但是,当列表中每个项目都有多个数据时,您可能需要考虑使用支持显示多个数据列的控件,例如<代码> ListVIEW < /COD>或<代码> DATAGRID控件。(为了在

列表视图
控件中显示列,您需要将
视图
属性设置为
详细信息
)。

XmlReader类对于某些任务很有用,但根据设计,它一次只检查一个节点。在这种情况下,您需要以随机访问方式同时访问多个元素,使用
XmlDocument
XDocument
类将更容易。例如:

Dim doc As New XmlDocument()
doc.Load("http://127.0.0.1/servers.xml")
For Each serverNode As XmlNode In doc.SelectNodes("/servers/server")
    Dim location As String = serverNode.SelectSingleNode("location").InnerText
    Dim ip As String = serverNode.SelectSingleNode("ip").InnerText
Next
lsbServers.Items.Add(location & " - " & ip)) 
或者

或:


但是,当列表中每个项目都有多个数据时,您可能需要考虑使用支持显示多个数据列的控件,例如<代码> ListVIEW < /COD>或<代码> DATAGRID控件。(为了在

列表视图
控件中显示列,您需要将
视图
属性设置为
详细信息
)。

我将使用LINQ to XML:

Dim xr As XmlReader = XmlReader.Create("http://127.0.0.1/servers.xml")
Dim xDoc As XDocument = XDocument.Load(xr)

For Each server as XElement In xDoc.Root.Elements("server")
    Dim location As String = CType(server.Element("location"), String)
    Dim ip As String = CType(server.Element("ip"), String)
    ' your ping logic goes here '
    Dim ping as String = GetPing(ip)

    lsbServers.Items.Add(String.Format("{0} - {1} - {2}", location, ip, ping))
Next

我将使用LINQ转换为XML:

Dim xr As XmlReader = XmlReader.Create("http://127.0.0.1/servers.xml")
Dim xDoc As XDocument = XDocument.Load(xr)

For Each server as XElement In xDoc.Root.Elements("server")
    Dim location As String = CType(server.Element("location"), String)
    Dim ip As String = CType(server.Element("ip"), String)
    ' your ping logic goes here '
    Dim ping as String = GetPing(ip)

    lsbServers.Items.Add(String.Format("{0} - {1} - {2}", location, ip, ping))
Next

你检查了吗?是的,但我不知道如何将IP+ping附加到IP到列表框中的位置。你检查了吗?是的,但我不知道如何将IP+ping附加到IP到列表框中的位置。