显示从Geolocation API的XML读取的信息

时间:2017-12-01 19:14:23

标签: xml vb.net

到目前为止,这是我的代码。我遇到了Dim ip As String = wxe.<Response>.<ip>行的问题。当我运行程序并输入IP地址时,它不会显示任何内容。

这是运行程序时抛出异常的图像:

enter image description here

这是一个来自URL的XML文件的图像,该程序应该从中提取信息。关于如何解决这个问题以及更好地说出这个问题的任何建议都受到欢迎。

enter image description here

免责声明 - 图片中使用的IP地址不是我自己的。这是我想到的随机IP地址。这不是特别的人。

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim ipAddress As String = txtIP.Text.Trim()
        'If IsNumeric(ipAddress) Then
        'Dim txtIP As String = Shell("cmd /c nslookup" & lblStatus.Text)
        Dim url As String = "http://freegeoip.net/xml/" & ipAddress
        Dim wxe As XElement = XElement.Load(url)
        Dim ip As String = wxe.<Response>.<ip>
        'lblStatus.Text = txtIP
        lblStatus.Text = ip
        'End If
    End Sub
End Class

2 个答案:

答案 0 :(得分:0)

工作示例:

...
        <dt>
            <br />

            <input type="submit" value="Log Off" class="btn btn-default" id="logoff" />
        </dt>
    </dl>
</div>
<div>
    <br />
    <table class="table" style="white-space: nowrap" id="results">

    </table>
</div>
<br />
<div id="button">

</div>
<p>
    @Html.ActionLink("Back to List", "Index")
</p>
<script>
    $('#logoff').click(function (event) {
        var SessionId = $('#SessionId').val();
        var ServerName = $('#ServerName').val();
        var AppName = $('#AppName').val();
        $(this).remove();
        lookfor = JSON.parse('{ "sessionid":"' + SessionId + '","servername":"' + ServerName + '" }');
        $.ajax({
            type: "POST",
            url: "/Home/LogOffSession",
            data: lookfor,
        }).done(function (data) {
            var html = "Logoff Failed"
            var err = "<tr><td>" + data.ErrorMessage + "</td></tr>"
            var btn = "<input type=\"submit\" value=\"Reset Profile\" class=\"btn btn-default\" id=\"resetelite\" />"

            if (err.length > 0) {
                $('#results').append($(err));

                $('#button').append($(btn));
            } else {
                $('#results').append($(html));
            }
        });
    });

    $(document).ready(function () {
        $(document).on("click", "resetelite", function (event) {
            var UserId = $('#UserId').val();
            $(this).remove();
            lookfor = JSON.parse('{ "username":"' + UserId + '" }');
            $.ajax({
                type: "POST",
                url: "/Home/ResetElite",
                data: lookfor,
            }).done(function (data) {
                var html = "<tr><td>" + data.Result + "</td></tr>"
                $('#results').html("");
                $('#results').append($(html));

            });
        });
    });

</script>

答案 1 :(得分:0)

试试xml linq:

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

        Dim doc As XDocument = XDocument.Load(URL)

        Dim results = doc.Descendants("Response").Select(Function(x) New With { _
                                          .ip = x.Element("IP").Value, _
                                          .code = x.Element("CountryCode").Value, _
                                          .countryName = x.Element("CountryName").Value, _
                                          .regionCode = x.Element("RegionCode").Value, _
                                          .regionName = x.Element("RegionName").Value, _
                                          .city = x.Element("City").Value,
                                          .zip = x.Element("ZipCode").Value, _
                                          .timeZone = x.Element("TimeZone").Value, _
                                          .latitude = Double.Parse(x.Element("Latitude").Value), _
                                          .longitude = Double.Parse(x.Element("Longitude").Value), _
                                          .metroCode = x.Element("MetroCode").Value _
                                      }).FirstOrDefault()

    End Sub