现在我得到了这个 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;
}