Results 1 to 6 of 6

Thread: Read XML Attribute

  1. #1

    Thread Starter
    New Member vrbahrami's Avatar
    Join Date
    Feb 2017
    Posts
    4

    Question Read XML Attribute

    Hi guys I'm a beginner, i have a problem to read a attribute from a XML file, How can i read sub attribute from repeated attribute?


    HTML Code:
    <weatherdata>
    <location>
    <name>Tehran</name>
    <type/>
    <country>IR</country>
    <timezone>12600</timezone>
    <location altitude="0" latitude="35.74" longitude="51.58" geobase="geonames" geobaseid="112931"/>
    </location>
    <credit/>
    <meta>
    <lastupdate/>
    <calctime>0</calctime>
    <nextupdate/>
    </meta>
    <sun rise="2020-10-05T02:31:49" set="2020-10-05T14:12:01"/>
    
    <forecast>
    
    <time from="2020-10-05T15:00:00" to="2020-10-05T18:00:00">
    <symbol number="803" name="broken clouds" var="04n"/>
    <precipitation probability="0.45"/>
    <windDirection deg="37" code="NE" name="NorthEast"/>
    <windSpeed mps="0.58" unit="m/s" name="Calm"/>
    <temperature unit="celsius" value="17.2" min="17.2" max="18.46"/>
    <feels_like value="15.9" unit="celsius"/>
    <pressure unit="hPa" value="1021"/>
    <humidity value="48" unit="%"/>
    <clouds value="broken clouds" all="57" unit="%"/>
    <visibility value="10000"/>
    </time>
    
    <time from="2020-10-05T18:00:00" to="2020-10-05T21:00:00">
    <symbol number="500" name="light rain" var="10n"/>
    <precipitation probability="0.36" unit="3h" value="0.2" type="rain"/>
    <windDirection deg="45" code="NE" name="NorthEast"/>
    <windSpeed mps="2" unit="m/s" name="Light breeze"/>
    <temperature unit="celsius" value="17.29" min="17.29" max="17.64"/>
    <feels_like value="14.75" unit="celsius"/>
    <pressure unit="hPa" value="1020"/>
    <humidity value="44" unit="%"/>
    <clouds value="broken clouds" all="60" unit="%"/>
    <visibility value="10000"/>
    </time>
    
    <time from="2020-10-05T21:00:00" to="2020-10-06T00:00:00">
    <symbol number="803" name="broken clouds" var="04n"/>
    <precipitation probability="0.08"/>
    <windDirection deg="109" code="ESE" name="East-southeast"/>
    <windSpeed mps="0.9" unit="m/s" name="Calm"/>
    <temperature unit="celsius" value="16.72" min="16.72" max="16.77"/>
    <feels_like value="14.72" unit="celsius"/>
    <pressure unit="hPa" value="1020"/>
    <humidity value="42" unit="%"/>
    <clouds value="broken clouds" all="58" unit="%"/>
    <visibility value="10000"/>
    </time>
    
    </forecast>
    </weatherdata>

    I use this code but allways return (name) from first block

    HTML Code:
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim e1 As String
            Dim m_xmld As XmlDocument = New XmlDocument
            m_xmld.Load("forecast.xml")
            Dim loc_node As XmlNode = m_xmld.SelectSingleNode("weatherdata/forecast/time/windSpeed")
            e1 = (loc_node.Attributes("name").Value)
            MessageBox.Show(e1)
        End Sub
    How Can Read (Name) Attribute from 2nd Block or 3rd Block?

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,543

    Re: Read XML Attribute

    Well, if SelectSingleNode returns a single node, and you want more than one node... what do you think you should do?
    Select multiple nodes... loop through them, and then get the attribute from each node.

    -tg

    hint: look up SelectNodes
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    New Member vrbahrami's Avatar
    Join Date
    Feb 2017
    Posts
    4

    Re: Read XML Attribute

    sorry I'm bigger and i don't understand your solution
    For Example feels_like value repeated on 3 block, how can read feels_like value on block 2 or 3?


    Attachment 178939

  4. #4
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,764

    Re: Read XML Attribute

    These sorts of things are better handled with XElement IMHO. I've commented out the .Load and used a literal for testing. What a 'block' exactly is I don't know. Try this code.

    Code:
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim wd As XElement
            'to load from a URI uncomment this and get rid of literal
            ' wd = XElement.Load("forecast.xml")
            '
            'a Literal for testing, delete when using .Load
            wd = <weatherdata>
                     <location>
                         <name>Tehran</name>
                         <type/>
                         <country>IR</country>
                         <timezone>12600</timezone>
                         <location altitude="0" latitude="35.74" longitude="51.58" geobase="geonames" geobaseid="112931"/>
                     </location>
                     <credit/>
                     <meta>
                         <lastupdate/>
                         <calctime>0</calctime>
                         <nextupdate/>
                     </meta>
                     <sun rise="2020-10-05T02:31:49" set="2020-10-05T14:12:01"/>
    
                     <forecast>
    
                         <time from="2020-10-05T15:00:00" to="2020-10-05T18:00:00">
                             <symbol number="803" name="broken clouds" var="04n"/>
                             <precipitation probability="0.45"/>
                             <windDirection deg="37" code="NE" name="NorthEast"/>
                             <windSpeed mps="0.58" unit="m/s" name="Calm"/>
                             <temperature unit="celsius" value="17.2" min="17.2" max="18.46"/>
                             <feels_like value="15.9" unit="celsius"/>
                             <pressure unit="hPa" value="1021"/>
                             <humidity value="48" unit="%"/>
                             <clouds value="broken clouds" all="57" unit="%"/>
                             <visibility value="10000"/>
                         </time>
    
                         <time from="2020-10-05T18:00:00" to="2020-10-05T21:00:00">
                             <symbol number="500" name="light rain" var="10n"/>
                             <precipitation probability="0.36" unit="3h" value="0.2" type="rain"/>
                             <windDirection deg="45" code="NE" name="NorthEast"/>
                             <windSpeed mps="2" unit="m/s" name="Light breeze"/>
                             <temperature unit="celsius" value="17.29" min="17.29" max="17.64"/>
                             <feels_like value="14.75" unit="celsius"/>
                             <pressure unit="hPa" value="1020"/>
                             <humidity value="44" unit="%"/>
                             <clouds value="broken clouds" all="60" unit="%"/>
                             <visibility value="10000"/>
                         </time>
    
                         <time from="2020-10-05T21:00:00" to="2020-10-06T00:00:00">
                             <symbol number="803" name="broken clouds" var="04n"/>
                             <precipitation probability="0.08"/>
                             <windDirection deg="109" code="ESE" name="East-southeast"/>
                             <windSpeed mps="0.9" unit="m/s" name="Calm"/>
                             <temperature unit="celsius" value="16.72" min="16.72" max="16.77"/>
                             <feels_like value="14.72" unit="celsius"/>
                             <pressure unit="hPa" value="1020"/>
                             <humidity value="42" unit="%"/>
                             <clouds value="broken clouds" all="58" unit="%"/>
                             <visibility value="10000"/>
                         </time>
    
                     </forecast>
                 </weatherdata>
    
            Dim ie As IEnumerable(Of XElement)
            'select all windSpeed nodes
            ie = From el In wd.<forecast>.<time>.<windSpeed>
                 Select el
    
            For Each el As XElement In ie
                Debug.WriteLine(el.@name)
                ' Stop
            Next
    
            'a specific Block????
            Dim block As XElement = ie(1)
        End Sub
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  5. #5
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,048

    Re: Read XML Attribute

    Quote Originally Posted by vrbahrami View Post
    Hi guys I'm a beginner, i have a problem to read a attribute from a XML file, How can i read sub attribute from repeated attribute?


    How Can Read (Name) Attribute from 2nd Block or 3rd Block?
    well why not use the Attribute(s) then

    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim x As XElement = XElement.Load("D:\TestWeather.xml")
            Dim getWinspeedData = x.Descendants("time")
            For Each xData In getWinspeedData
                Debug.WriteLine(xData.Elements("windSpeed").Attributes("name").ElementAt(0).Value)
                Debug.WriteLine(xData.Elements("windSpeed").Attributes("mps").ElementAt(0).Value)
                Debug.WriteLine(xData.Elements("windSpeed").Attributes("unit").ElementAt(0).Value)
                Debug.WriteLine("------------------------------------")
            Next xData
    End Sub
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  6. #6
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,543

    Re: Read XML Attribute

    Quote Originally Posted by vrbahrami View Post
    sorry I'm bigger and i don't understand your solution
    For Example feels_like value repeated on 3 block, how can read feels_like value on block 2 or 3?


    Attachment 178939
    SelectSingleNode does just what it states... selects a single node... you don't want a single node... you want multiple nodes... use SelectNodes...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width