【问题标题】:How to call specific line in HTTP Get from XML如何从 XML 调用 HTTP Get 中的特定行
【发布时间】:2012-12-18 18:16:37
【问题描述】:

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

【问题讨论】:

    标签: c# xml web-services http httpwebrequest


    【解决方案1】:

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

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

    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);
    

    【讨论】:

    • 感谢您的回复,在使用您的代码时我应该删除/保留哪些部分代码
    • 我刚刚编辑了答案,添加了您发布的所有代码,并在 cmets 之间标记了插入。
    • 谢谢,我在这一行下遇到了一个错误...... var doc = XDocument.Load(myHttpWebResponse.GetResponseStream());
    • 它是这么说的... 错误 2 参数 1:无法从 'System.IO.Stream' 转换为 'System.Xml.XmlReader' 还有这个.. 错误 1 ​​最好的重载方法匹配'System.Xml.Linq.XDocument.Load(System.Xml.XmlReader)' 有一些无效参数
    • 请注意,传递给XDocument.Load() 的参数不是代码中的myXMLReader 变量,而是myHttpWebResponse.GetResponseStream()
    【解决方案2】:

    我会这样做:

         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 具有更高的性能,但代码非常小且易于维护。

    【讨论】:

    • 感谢您的回复。但是,在我点击提交按钮后,我如何让它显示在我的名为 txtResults.Text 的 txt 框中
    • 刚刚编辑了我的答案,只需将其放在您的按钮提交点击中,不要忘记添加所有需要的参考。
    • 我在这一行下遇到错误... txtIPresults.Text = name;错误是:错误1无法将类型'System.Xml.XmlNode'隐式转换为'string'
    • 忘记在选择单节点上添加 .Value,现在应该可以了。
    • 谢谢。嗯,错误消失了,但现在当我点击提交按钮时,什么也没有弹出。
    猜你喜欢
    • 1970-01-01
    • 2010-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多