我有一个包含余额的文本框和另一个我希望以欧元显示余额的文本框。当我点击转换按钮时,我希望它能够将英镑兑换成欧元。
那么如何让我的余额显示在streling中?如何让它转换为欧元呢?
任何示例代码都会很棒,甚至可以将我引导到一个可以教我这个的网站。
答案 0 :(得分:2)
欧洲中央银行(ECB)每天以XML格式提供货币汇率
货币汇率链接为here
使用webrequest保存此xml并编写包装类以进行转换
如果你想使用xe.com使用这个link,它有三个参数 1.要转换,2。从货币和3.到货币
该链接的输出是
<wml>
<head>
<meta http-equiv="Cache-Control" content="must-revalidate" forua="true"/>
<meta http-equiv="Cache-Control" content="no-cache" forua="true"/>
</head>
<card title="XE Converter">
<p mode="wrap" align="center">
XE Converter
</p>
<p mode="nowrap" align="left">100 SGD =</p>
<p mode="wrap" align="right">18,287.95 HUF</p>
<p mode="wrap" align="center">
Live @ 12:07 GMT
</p>
<p mode="nowrap" align="left">
<a href="step1.wml">Another?</a><br/>
<a href="http://www.xe.com/wap/index.wml">XE Home</a>
</p>
</card>
</wml>
再次使用webrequest类并获取输出并解析该xml
示例在这里
HttpWebRequest myHttpWebRequest = null; //Declare an HTTP-specific implementation of the WebRequest class.
HttpWebResponse myHttpWebResponse = null; //Declare an HTTP-specific implementation of the WebResponse class
XmlDocument myXMLDocument = null; //Declare XMLResponse document
XmlTextReader myXMLReader = null; //Declare XMLReader
try
{
//Create Request
myHttpWebRequest = (HttpWebRequest)HttpWebRequest.Create("http://www.xe.com/wap/2co/convert.cgi?Amount=100&From=SGD&To=HUF");
myHttpWebRequest.Method = "GET";
myHttpWebRequest.ContentType = "text/xml; encoding='utf-8'";
//Get Response
myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
//Now load the XML Document
myXMLDocument = new XmlDocument();
//Load response stream into XMLReader
myXMLReader = new XmlTextReader(myHttpWebResponse.GetResponseStream());
myXMLDocument.Load(myXMLReader);
}
catch (Exception myException)
{
throw myException;
}
finally
{
myHttpWebRequest = null;
myHttpWebResponse = null;
myXMLReader = null;
}