2011-12-13 27 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> 

相關問題