SeaSide SeaSide - 1 year ago 68 Question

How to get attribut value from xml element?

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

<gesmes:Envelope xmlns:gesmes="" xmlns="">
<Cube time="2016-08-16">
<Cube value="1" />
<Cube value="2"/>
<Cube value="3"/>

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

Answer Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download