0

现在我得到了这个 XML 的完整转储......

http://smart-ip.net/geoip-xml/68.5.63.33

我希望我的程序做的只是从那个 XML 中调用城市和地区。

我是网络服务的新手,所以我很难弄清楚如何做到这一点,非常感谢帮助

这是我的代码:

HttpWebRequest myHttpWebRequest = null;
HttpWebResponse myHttpWebResponse = null;
XmlTextReader myXMLReader = null;

try
{
    XPathNavigator nav;
    XPathDocument docNav;

    String weatherURL = "http://smart-ip.net/geoip-xml/" + txtIP.Text;

    myHttpWebRequest = (HttpWebRequest)HttpWebRequest.Create(weatherURL);
    myHttpWebRequest.Method = "GET";
    myHttpWebRequest.ContentType = "text/xml; encoding='utf-8'";
    myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
    myXMLReader = new XmlTextReader(myHttpWebResponse.GetResponseStream());

    docNav = new XPathDocument(myXMLReader);

    nav = docNav.CreateNavigator();
    nav.MoveToRoot();
    nav.MoveToFirstChild();

    do
    {
        if (nav.NodeType == XPathNodeType.Element)
        {
            nav.MoveToFirstChild();
            do
            {
                txtIPresults.Text = txtIPresults.Text + nav.Name + " - " + nav.Value + Environment.NewLine;  //Display
            } while (nav.MoveToNext());
        }
    } while (nav.MoveToNext());
}
catch (Exception myException)
{
    throw new Exception("Error Occurred:", myException);
}
finally
{
    myHttpWebRequest = null;
    myHttpWebResponse = null;
    myXMLReader = null;
}
4

2 回答 2

0

如果我理解正确,您正试图从 XML 响应中的countryNameand元素中获取值。city

这可以使用命名空间中的XDocument类通过以下方式完成System.Xml.Linq

HttpWebRequest myHttpWebRequest = null;
HttpWebResponse myHttpWebResponse = null;

try
{
    String weatherURL = "http://smart-ip.net/geoip-xml/" + txtIP.Text;

    myHttpWebRequest = (HttpWebRequest)HttpWebRequest.Create(weatherURL);
    myHttpWebRequest.Method = "GET";
    myHttpWebRequest.ContentType = "text/xml; encoding='utf-8'";
    myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();

    //---- <Added code> -------

    var doc = XDocument.Load(myHttpWebResponse.GetResponseStream());

    var geoip = doc.Element("geoip");
    var country = geoip.Element("countryName").Value;
    var city = geoip.Element("city").Value;

    Console.WriteLine(country + " - " + city);

    //---- </Added code> -------
}
catch (Exception myException)
{
    throw new Exception("Error Occurred:", myException);
}
finally
{
    myHttpWebRequest = null;
    myHttpWebResponse = null;
}

也可以使用该XDocument.Load()方法直接使用 url 字符串加载 XML 响应:

String weatherURL = "http://smart-ip.net/geoip-xml/" + txtIP.Text;
var doc = XDocument.Load(weatherURL);
于 2012-12-18T18:32:12.173 回答
0

我会做这样的事情:

     try
     {
        WebClient wc = new WebClient();
        wc.Headers.Add();//ADD ALL YOUR HEADERS IF YOU NEED
        var xml = wc.DownloadString(string.Format("http://smart-ip.net/geoip-xml/{0}", txtIP.Text));

        XmlDocument doc = new XmlDocument();
        doc.LoadXml(xml);

        var name = doc.DocumentElement.SelectSingleNode("//countryName").Value;
        txtIPresults.Text = name
     }
     catch (Exception myException)
     {
        throw new Exception("Error Occurred:", myException);
     }

我不确定它是否比 HTTP REQUEST/RESPONSE 具有更高的性能,但代码非常小且易于维护。

于 2012-12-18T18:59:58.580 回答