1

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
4
  • Please post the xml you are attempting to extract from and the code where you create xmlTree1. Commented Sep 14, 2016 at 10:25
  • I have put the code of the XML and code where I create xmlTree1. Commented Sep 14, 2016 at 10:50
  • 2
    The gesmes prefix needs to be registered with the namespace. See this article on using the namespace manager support.microsoft.com/en-us/kb/318545 Commented Sep 14, 2016 at 10:53
  • I resolved. Thank you for your answer. Commented Sep 14, 2016 at 13:17

2 Answers 2

1

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).

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your answer Heinzi. This will work with NET.Framework 4 but currently I am using NET.Framework 2 and it doesn't recognize XDocument.
@SeaSide: .NET Framework 3.5 should work as well, if that is an option for you.
That's good to know, because I will certainly have to upgrade on the newer. Thank you.
1

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                                                                        

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.