0

現在、この XML の完全なダンプを取得しています...

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

私がプログラムに実行させたいのは、その XML から都市と地域を呼び出すことです。

私はWebサービスを初めて使用するので、これを行う方法を理解するのに苦労しています。助けていただければ幸いです

これが私のコードです:

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 応答からcountryNameおよび要素から値を取得しようとしています。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 に答える