WEBCLIENT 连接到“欧洲中央银行”以计算国家之间的汇率。






3.57/5 (14投票s)
2005年4月5日
2分钟阅读

62358

936
这段代码加载 “ECB” xml 文件并将其解析到数据集。由此,我们可以计算不同国家之间的汇率。
引言
欧洲央行非常方便地(并且免费)每天提供大多数
常用货币的汇率,以多种文件格式提供,其中包括用于解析的 XML 文件。(缩短的副本如下)
<?xml version="1.0" encoding="UTF-8"?>
<gesmes:Envelope xmlns:gesmes="http://www.gesmes.org/xml/2002-08-01" xmlns="http://www.ecb.int/vocabulary/2002-08-01/eurofxref">
<gesmes:subject>参考汇率</gesmes:subject>
<gesmes:Sender>
<gesmes:name>欧洲中央银行</gesmes:name>
</gesmes:Sender>
<Cube>
<Cube time='2004-03-11'>
<Cube currency='USD' rate='1.2256'/>
<Cube currency='JPY' rate='135.58'/>
<Cube currency='DKK' rate='7.4518'/>
....
<Cube currency='ZAR' rate='8.2441'/>
</Cube>
</Cube>
</gesmes:Envelope>
由于这是一个免费资源,因此我也能发布 XML 文件的 URI
http://www.ecb.int/stats/eurofxref/eurofxref-hist.xml
我将创建一个 Windows 应用程序,它将作为连接到 "ECB" XML 的 Web 客户端。
利用欧洲中央银行的实际汇率,制作一个计算器来计算不同国家之间的汇率。
背景
基本思想是创建一个程序来计算不同国家之间的汇率。欧洲中央银行提供了一个 XML 文件,其中包含典型的
贸易国家汇率。由此,我可以计算出我必须为产品支付多少钱。请记住,我们只能获取
银行大约在下午 4 点关闭时的实际汇率。
使用代码
Microsoft 使用 XmlTextReader,它可以从任何类型的 XML 文件中读取数据。
但 ECB xml 文件不是真正的 Web 服务,因此我想将其解析为一个数据集,如下所述。
在这个页面 http://www.oanda.com/site/help/iso_code.shtml 可以找到按国家和贵金属列出的货币 ISO 代码索引列表。您还可以通过代码查找 ISO 货币代码。请参阅图片了解 Web 客户端的工作原理。
country and precious metal. You can also find an ISO currency code by code. See picture how Web Client works.
我将解析数据存储到数据集中,请参见图片。
从此数据集中,我可以查找和获取数据,等等。
代码
private ExchanceRate parseWebXML(string WebAddress) { XmlTextReader xmlReader; ExchanceRate dsVa = new ExchanceRate(); DataRow newRowVa = null; try { //Read data from the XML-file over the interNET xmlReader = new XmlTextReader(WebAddress); } catch( WebException ) { throw new WebException("There has been a mistake with collect the xmlfile from the net!"); } try { while( xmlReader.Read() ) { if (xmlReader.Name != "") { //Check that there are node call gesmes:name if (xmlReader.Name == "gesmes:name") { _author = xmlReader.ReadString(); } for (int i = 0 ; i < xmlReader.AttributeCount; i++) { //Check that there are node call Cube if (xmlReader.Name == "Cube") { //Check that there are 1 attribut, then get the date if (xmlReader.AttributeCount == 1) { xmlReader.MoveToAttribute("time"); DateTime tim = DateTime.Parse(xmlReader.Value); newRowVa = null; DataRow newRowCo = null; newRowVa = dsVa.Exchance.NewRow(); newRowVa["Date"]= tim; dsVa.Exchance.Rows.Add(newRowVa); newRowCo = dsVa.Country.NewRow(); newRowCo["Initial"]= "EUR"; newRowCo["Name"]= convert.MoneyName("EUR"); // Find Country name from ISO code newRowCo["Rate"]= 1.0; dsVa.Country.Rows.Add(newRowCo); newRowCo.SetParentRow(newRowVa); // Make Key to subtable } //If the number of attributs are 2, so get the ExchangeRate-node if (xmlReader.AttributeCount == 2) { xmlReader.MoveToAttribute("currency"); string cur = xmlReader.Value; xmlReader.MoveToAttribute("rate"); decimal rat = decimal.Parse(xmlReader.Value.Replace(".",",")); // I am using "," as a decimal symbol DataRow newRowCo = null; newRowCo = dsVa.Country.NewRow(); newRowCo["Initial"]= cur; newRowCo["Name"]= convert.MoneyName(cur); newRowCo["Rate"]= rat; dsVa.Country.Rows.Add(newRowCo); newRowCo.SetParentRow(newRowVa); } xmlReader.MoveToNextAttribute(); } } } } catch( WebException ) { throw new WebException("connections lost"); } return dsVa; }
在这里,我可以根据汇率计算不同国家之间的汇率。
private static double ExRateValue(double Input, double FromContry, double ToContry) { double result = 0; if( FromContry == 0 ) return 0; result = (Input/FromContry)*ToContry; result = Math.Round(result,2); return result; }
结论
本文展示了一种如何实现通过互联网连接的简单解决方案。
我使用了 .Net 技术的强大功能,例如 XmlTextReader 中的 Web 客户端。
请记住,使用此链接可以获取从欧洲中央银行历史记录 01/04/1999 开始的数据,
在我的计算机上,加载 xml 并将数据解析到我的数据集需要 ½ 秒。
祝你好运。