Reputation: 149
I have the following 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>Reference rates</gesmes:subject>
<gesmes:Sender>
<gesmes:name>European Central Bank</gesmes:name>
</gesmes:Sender>
<Cube>
<Cube time='2016-09-12'>
<Cube currency='USD' rate='1.1226'/>
<Cube currency='JPY' rate='114.38'/>
</Cube>
</Cube>
</gesmes:Envelope>
I want to get attribute value of each currency. For now I am using this but it doesn't work:
Dim xmlTree1 As New XmlDocument()
xmlTree1.Load("C:\\download\eurofxref-daily.xml")
Dim currencyUSD As String = xmlTree1.SelectSingleNode("/gesmes:Envelope/Cube/Cube/Cube[@currency='USD']/@rate").Value
Dim currencyJPY As String = xmlTree1.SelectSingleNode("/gesmes:Envelope/Cube/Cube/Cube[@currency='JPY']/@rate").Value
Upvotes: 1
Views: 2101
Reputation: 172270
An alternative is to use the awesome Linq-to-XML features of VB.NET (Framework 3.5 and higher):
Imports <xmsns:xref="http://www.ecb.int/vocabulary/2002-08-01/eurofxref">
...
Dim xdoc = XDocument.Load("C:\kursna_standalone\download\eurofxref-daily.xml")
Dim cubes = xdoc.Root.<xref:Cube>.<xref:Cube>
Dim currencyUSD = cubes.Where(Function(x) x.currency = "USD").@rate
Dim currencyJPY = cubes.Where(Function(x) x.currency = "JPY").@rate
(Note: All my VB code examples assume that Option Strict and Option Infer are active).
Upvotes: 1
Reputation: 149
I am using the following code to get the value from xml and it works:
Dim xmlTree1 As New XmlDocument()
xmlTree1.Load("C:\\kursna_standalone\download\eurofxref-daily.xml")
Dim xmlnsManager1 As New System.Xml.XmlNamespaceManager(xmlTree1.NameTable)
xmlnsManager1.AddNamespace("gm1", "http://www.ecb.int/vocabulary/2002-08-01/eurofxref")
Dim currencyUSD As String = xmlTree1.SelectSingleNode("//gm1:Cube/gm1:Cube/gm1:Cube[@currency='USD']/@rate", xmlnsManager1).Value
Dim currencyJPY As String = xmlTree1.SelectSingleNode("//gm1:Cube/gm1:Cube/gm1:Cube[@currency='JPY']/@rate", xmlnsManager1).Value
Upvotes: 1