XML nodes ?

CdRsKuLL

Active member
Joined
Sep 26, 2006
Messages
40
Programming Experience
Beginner
Hiya,

I'm slowly getting to grips with XML files and I'm trying to read a weather xml feed. I'm struggling with elements that are called the same (weather). I want to read bits into strings.

OK, here's a copy of the xml

VB.NET:
-<data><request><type>LatLon</type><query>Lat -0.13 and Lon 51.51</query></request><current_condition><observation_time>08:42 AM</observation_time><temp_C>30</temp_C><temp_F>85</temp_F><weatherCode>113</weatherCode><weatherIconUrl>http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0001_sunny.png</weatherIconUrl><weatherDesc>Sunny</weatherDesc><windspeedMiles>15</windspeedMiles><windspeedKmph>24</windspeedKmph><winddirDegree>144</winddirDegree><winddir16Point>SE</winddir16Point><precipMM>0.1</precipMM><humidity>76</humidity><visibility>10</visibility><pressure>1011</pressure><cloudcover>22</cloudcover></current_condition><weather><date>2012-10-12</date><tempMaxC>30</tempMaxC><tempMaxF>86</tempMaxF><tempMinC>26</tempMinC><tempMinF>78</tempMinF><windspeedMiles>22</windspeedMiles><windspeedKmph>35</windspeedKmph><winddirection>SE</winddirection><winddir16Point>SE</winddir16Point><winddirDegree>134</winddirDegree><weatherCode>116</weatherCode><weatherIconUrl>http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0002_sunny_intervals.png</weatherIconUrl><weatherDesc>Partly Cloudy </weatherDesc><precipMM>1.2</precipMM></weather><weather><date>2012-10-13</date><tempMaxC>29</tempMaxC><tempMaxF>85</tempMaxF><tempMinC>26</tempMinC><tempMinF>78</tempMinF><windspeedMiles>14</windspeedMiles><windspeedKmph>23</windspeedKmph><winddirection>SSE</winddirection><winddir16Point>SSE</winddir16Point><winddirDegree>165</winddirDegree><weatherCode>116</weatherCode><weatherIconUrl>http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0002_sunny_intervals.png</weatherIconUrl><weatherDesc>Partly Cloudy </weatherDesc><precipMM>0.3</precipMM></weather><weather><date>2012-10-14</date><tempMaxC>29</tempMaxC><tempMaxF>84</tempMaxF><tempMinC>25</tempMinC><tempMinF>77</tempMinF><windspeedMiles>13</windspeedMiles><windspeedKmph>22</windspeedKmph><winddirection>SSE</winddirection><winddir16Point>SSE</winddir16Point><winddirDegree>158</winddirDegree><weatherCode>116</weatherCode><weatherIconUrl>http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0002_sunny_intervals.png</weatherIconUrl><weatherDesc>Partly Cloudy </weatherDesc><precipMM>5.5</precipMM></weather><weather><date>2012-10-15</date><tempMaxC>27</tempMaxC><tempMaxF>80</tempMaxF><tempMinC>25</tempMinC><tempMinF>78</tempMinF><windspeedMiles>10</windspeedMiles><windspeedKmph>16</windspeedKmph><winddirection>SSE</winddirection><winddir16Point>SSE</winddir16Point><winddirDegree>149</winddirDegree><weatherCode>353</weatherCode><weatherIconUrl>http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0009_light_rain_showers.png</weatherIconUrl><weatherDesc>Light rain shower</weatherDesc><precipMM>15.4</precipMM></weather><weather><date>2012-10-16</date><tempMaxC>28</tempMaxC><tempMaxF>82</tempMaxF><tempMinC>26</tempMinC><tempMinF>78</tempMinF><windspeedMiles>3</windspeedMiles><windspeedKmph>5</windspeedKmph><winddirection>WSW</winddirection><winddir16Point>WSW</winddir16Point><winddirDegree>237</winddirDegree><weatherCode>116</weatherCode><weatherIconUrl>http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0002_sunny_intervals.png</weatherIconUrl><weatherDesc>Partly Cloudy </weatherDesc><precipMM>0.1</precipMM></weather></data>

Now it has 5 elements all called weather. So I've used this code...

VB.NET:
        xml = webclient.DownloadString("http://free.worldweatheronline.com/feed/weather.ashx?q=" & lat & "," & lon & "&format=xml&num_of_days=5&key=removedthekey")
        DataXml.LoadXml(xml)
        With newweather
            .wind = DataXml.DocumentElement.SelectSingleNode("//data/current_condition/windspeedMiles").InnerText
            .winddirection = DataXml.DocumentElement.SelectSingleNode("//data/current_condition/winddir16Point").InnerText
            .humidity = DataXml.DocumentElement.SelectSingleNode("//data/current_condition/humidity").InnerText
            .visibility = DataXml.DocumentElement.SelectSingleNode("//data/current_condition/visibility").InnerText
            .weathercon = DataXml.DocumentElement.SelectSingleNode("//data/current_condition/weatherDesc").InnerText
            Dim nodes As XmlNodeList = DataXml.DocumentElement.SelectNodes("//data/weather")
            .weathercon1 = nodes(1).SelectSingleNode("//weatherDesc").InnerText
            .weathercon2 = nodes(2).SelectSingleNode("//weatherDesc").InnerText
            .weathercon3 = nodes(3).SelectSingleNode("//weatherDesc").InnerText
            .weathercon4 = nodes(4).SelectSingleNode("//weatherDesc").InnerText

        End With

But all the weathercon's are the same value so I'm missing something. When I do nodes.count it says 5 so I know it's populated correctly. The other problem is I can't debug as it's part of a dll.

thanks,

Steve
 
Hi,

I am not an XML expert, so be gentle with me if you have further questions, but your issue here is that you have referenced the root in your "//weatherDesc" string. It should just be "weatherDesc".

Hope that helps.

Cheers,

Ian
 

Latest posts

Back
Top