Visual Basic' System.Xml.XmlException'发生在System.Xml.dll错误

时间:2015-06-11 16:50:14

标签: c# xml vb.net

我正在尝试制作代码以获取IP GEO信息。

这是我的代码:

Imports System.Xml
Public Class Form1

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
End Sub

Private Sub geo()
    Dim xmldoc As New XmlDocument
    Dim xmlnode As XmlNodeList
    Dim i As Integer
    xmldoc.Load("http://freegeoip.cnet/xml/" & TextBox1.Text)// this is where I get the Error
    xmlnode = xmldoc.GetElementsByTagName("Respon­se")
    For i = 0 To xmlnode.Count - 1
        Label1.Text = "IP Address : " & xmlnode(i).ChildNodes.Item(0).InnerText.Trim()
        Label2.Text = "Country Code : " & xmlnode(i).ChildNodes.Item(0).InnerText.Trim()
        Label3.Text = "Country Name : " & xmlnode(i).ChildNodes.Item(1).InnerText.Trim()
        Label4.Text = "Region Code : " & xmlnode(i).ChildNodes.Item(2).InnerText.Trim()
        Label5.Text = "Region Name : " & xmlnode(i).ChildNodes.Item(3).InnerText.Trim()
        Label6.Text = "City : " & xmlnode(i).ChildNodes.Item(4).InnerText.Trim()
        Label7.Text = "Zip Code : " & xmlnode(i).ChildNodes.Item(5).InnerText.Trim()
        Label8.Text = "Latitude : " & xmlnode(i).ChildNodes.Item(6).InnerText.Trim()
        Label9.Text = "Longitude : " & xmlnode(i).ChildNodes.Item(7).InnerText.Trim()
        Label10.Text = "Metro Code : " & xmlnode(i).ChildNodes.Item(8).InnerText.Trim()
        xmlnode(i).ChildNodes.Item(9).InnerText.Trim()

    Next

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    geo()
End Sub
End Class

这是错误消息:

  

' System.Xml.XmlException'发生在System.Xml.dll

任何人都知道为什么?

1 个答案:

答案 0 :(得分:0)

您没有将正确的参数传递给网站。以下是从freegeoip返回的数据示例。

Imports System.Xml
Module Module1

    Sub Main()
        Dim input As String =
            "<?xml version=""1.0"" encoding=""UTF-8""?>" & _
               "<Response>" & _
                  "<IP>123.123.123.123</IP>" & _
                  "<CountryCode>CN</CountryCode>" & _
                  "<CountryName>China</CountryName>" & _
                  "<RegionCode>11</RegionCode>" & _
                  "<RegionName>Beijing Shi</RegionName>" & _
                  "<City>Beijing</City>" & _
                  "<ZipCode/>" & _
                  "<TimeZone>Asia/Shanghai</TimeZone>" & _
                  "<Latitude>39.929</Latitude>" & _
                  "<Longitude>116.388</Longitude>" & _
                  "<MetroCode>0</MetroCode></Response>"
        Dim xmldoc As New XmlDocument
        xmldoc.LoadXml(input)

    End Sub

End Module​