By trial and error, I managed to extract exchange rates from the official ECB (European Central Bank) website;
Following piece of code was used:
Access to the life website has been commented out and replaced by a Dim definition, so that one can see how the website looks like.
I have two problems with this "solution":
1. As you can see, the element names are fully qualified. However, I got hopelessly entangled while trying to use namespaces properly in VB (by imporing them and then trying to use their prefixes)
2. the variable EurToX does not seem to exist outside the major For .. root.elements loop.
Can somebody sort this out?
_________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
Following piece of code was used:
Code:
Imports System.Xml.Linq
Imports <xmlns:gesmes="[URL unfurl="true"]http://www.gesmes.org/xml/2002-08-01">[/URL]
Imports <xmlns="[URL unfurl="true"]http://www.ecb.int/vocabulary/2002-08-01/eurofxref">[/URL]
Module Module1
Sub Main()
'Dim strXrateUrl As String = "[URL unfurl="true"]http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml"[/URL]
'Dim docXrate As XDocument = XDocument.Load(strXrateUrl)
'Console.WriteLine(docXrate.ToString)
Dim strEcbRates = _
<?xml version="1.0" encoding="UTF-8"?>
<gesmes:Envelope xmlns:gesmes="[URL unfurl="true"]http://www.gesmes.org/xml/2002-08-01"[/URL] xmlns="[URL unfurl="true"]http://www.ecb.int/vocabulary/2002-08-01/eurofxref">[/URL]
<gesmes:subject>Reference rates</gesmes:subject>
<gesmes:Sender>
<gesmes:name>European Central Bank</gesmes:name>
</gesmes:Sender>
<Cube>
<Cube time="2010-07-14">
<Cube currency="USD" rate="1.2703"/>
<Cube currency="JPY" rate="112.49"/>
<Cube currency="BGN" rate="1.9558"/>
<Cube currency="CZK" rate="25.470"/>
<Cube currency="THB" rate="41.050"/>
<Cube currency="ZAR" rate="9.5960"/>
</Cube>
</Cube>
</gesmes:Envelope>
Dim root = strEcbRates.Element("{[URL unfurl="true"]http://www.gesmes.org/xml/2002-08-01}Envelope")[/URL]
For Each el In root.Elements
'Search and retain the outer Cube element
Select Case el.Name = "{[URL unfurl="true"]http://www.ecb.int/vocabulary/2002-08-01/eurofxref}Cube"[/URL]
Case True
Dim xelCube = el 'Outer Cube, has one dependent element, also named Cube
'it has a Time attribute (which is a date!)
Dim xelTime = xelCube.Element("{[URL unfurl="true"]http://www.ecb.int/vocabulary/2002-08-01/eurofxref}Cube")[/URL]
'Elements with exchange rates are also called Cube!
Dim EuroToX = _
From x In xelTime.Elements _
Select New With {.Currency = x.Attribute("currency").Value, _
.XRate = x.Attribute("rate").Value}
Case False
'Do Nothing
End Select
Next
End Sub
End Module
Access to the life website has been commented out and replaced by a Dim definition, so that one can see how the website looks like.
I have two problems with this "solution":
1. As you can see, the element names are fully qualified. However, I got hopelessly entangled while trying to use namespaces properly in VB (by imporing them and then trying to use their prefixes)
2. the variable EurToX does not seem to exist outside the major For .. root.elements loop.
Can somebody sort this out?
_________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]