0

I have the following xml which I take from specified url:

<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:Sender>
    <Cube>
        <Cube time="2016-08-16">
           <Cube value="1" />
           <Cube value="2"/>
           <Cube value="3"/>
        </Cube>
    </Cube>
</gesmes:Envelope>

From this file I only need to take time value '2016-08-16' using Visual Basic code.

2 Answers 2

1

Try xml linq

Imports System.Xml
Imports System.Xml.Linq
Module Module1
    Const FILENAME As String = "c:\temp\test.xml"
    Sub Main()
        Dim doc As XDocument = XDocument.Load(FILENAME)
        Dim _date As DateTime = CType(doc.Descendants().Where(Function(x) (x.Name.LocalName = "Cube") And (Not x.Attribute("time") Is Nothing)).Select(Function(x) x.Attribute("time")).FirstOrDefault(), DateTime)
    End Sub

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

Comments

1

Add these imports:

Imports <xmlns:gesmes="http://www.gesmes.org/xml/2002-08-01">
Imports <xmlns="http://www.ecb.int/vocabulary/2002-08-01/eurofxref">

Then load your data however you wish into an XDocument or XElement

Dim xmlDoc = XDocument.Load("myxml.xml")

The query it as follows. (if you're using XElement don't include Root).

Dim time = xmlDoc.Root.<Cube>.<Cube>.@time

2 Comments

If you use an XDocument you need to access the Root property first (and I'm not sure the Load will work without an xml declaration on the sample). OTOH you can use an XElement instead
Thanks @Sehnsucht I've added the missing Root. I developed it using XElement and then got caught out with the shortcut.

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.