-
Oct 6th, 2020, 09:28 AM
#1
Thread Starter
New Member
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?
-
Oct 6th, 2020, 11:01 AM
#2
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
-
Oct 6th, 2020, 11:24 AM
#3
Thread Starter
New Member
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
-
Oct 6th, 2020, 11:48 AM
#4
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
-
Oct 6th, 2020, 12:04 PM
#5
Re: Read XML Attribute
Originally Posted by vrbahrami
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.
-
Oct 6th, 2020, 01:35 PM
#6
Re: Read XML Attribute
Originally Posted by vrbahrami
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
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|