2011-12-13 59 views
0

这是我尝试使用的Web服务。我正在使用Visual Studio 2010. http://www.webservicex.net/country.asmx使用XML从Web服务调用国家/地区的货币名称

这是我的代码。我已将网络服务添加到我的网站,并且在我的环境中没有显示错误。当我运行程序并点击我的按钮时,然而,没有任何反应。

using System; 
using System.Collections.Generic; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Xml; 
using System.Xml.XPath; 
using System.Net; 
using System.Web.Services.Protocols; 

public partial class _Default : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 

    } 

    public void btnCountry_Click(object sender, EventArgs e) 
    { 
     CountryInfo.country country = new CountryInfo.country(); 
     String countryName = txtCountry.Text; 
     String currencyName = country.GetCurrencyByCountry(countryName); 

     XPathNavigator nav; 
     XmlDocument myDoc = new XmlDocument(); 

     myDoc.LoadXml(currencyName); 
     nav = myDoc.CreateNavigator(); 

     lblCurrency.Text = nav.SelectSingleNode("//Currency").Value; 
     String countryCode = nav.SelectSingleNode("//CurrencyCode").Value; 
    } 
} 

任何帮助将不胜感激!

回答

0

我试过上面的例子,我得到了没有任何问题的回应。只要确保您作为参数传递给Web服务的国家名称需要位于Web服务公开的国家/地区列表中。您有一个Web服务公开的方法“GetCountries”。如果一个国家没有数据,那么它就是一个空的数据集。

只是尝试使用国家名称值“英国”,我可以看到如下的回应:

<?xml version="1.0" encoding="utf-8" ?> 
    <string xmlns="http://www.webserviceX.NET"> 
<NewDataSet> 
    <Table> 
     <Name>United Kingdom</Name> 
     <CountryCode>uk</CountryCode> 
     <Currency>Pound</Currency> 
     <CurrencyCode>GBP</CurrencyCode> 
    </Table> 
    <Table> 
     <Name>United Kingdom</Name> 
     <CountryCode>uk</CountryCode> 
     <Currency>Pound</Currency> 
     <CurrencyCode>GBP</CurrencyCode> 
    </Table> 
</NewDataSet> 

相关问题