By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,652 Members | 1,358 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,652 IT Pros & Developers. It's quick & easy.

how can i read the nodes of this file in asp.net???

P: n/a
hi there,

i have an xml file, but am not too sure how to read all these elements using
vb.net code.

any help would be greatly appreciated:))
<---------CODE--------------->

<?xml version="1.0" encoding="utf-8"?>
<weatherFeed>
<CurrentConditions>
<country>Albania</country>
<city>Tirana</city>
<weatherImageURL>cloudy.gif</weatherImageURL>
<celcius>14C</celcius>
<farenheit>57F</farenheit>
<description>Partly Cloudy</description>
<relativeHumidity>61%</relativeHumidity>
<wind>WSW at 10 mph (16 km/h)</wind>
<sunrise>7:05 AM</sunrise>
<sunset>4:36 PM</sunset>
<barometricPressure>26.1Hg (F)</barometricPressure>
<lastUpdated>15/01/2004 13:21:14</lastUpdated>
<!--5 Day Weather Forecast-->
<fiveDayForecast>
<Day1>
<day>Thursday</day>
<highCelcius>14C</highCelcius>
<lowCelcius>1C</lowCelcius>
<highFarenheit>58F</highFarenheit>
<lowFarenheit>33F</lowFarenheit>
<imageURL>cloudy.gif</imageURL>
</Day1>
<Day2>
<day>Friday</day>
<highCelcius>11C</highCelcius>
<lowCelcius>1C</lowCelcius>
<highFarenheit>51F</highFarenheit>
<lowFarenheit>34F</lowFarenheit>
<imageURL>cloudy.gif</imageURL>
</Day2>
<Day3>
<day>Saturday</day>
<highCelcius>11C</highCelcius>
<lowCelcius>1C</lowCelcius>
<highFarenheit>51F</highFarenheit>
<lowFarenheit>33F</lowFarenheit>
<imageURL>cloudy.gif</imageURL>
</Day3>
<Day4>
<day>Sunday</day>
<highCelcius>9C</highCelcius>
<lowCelcius>-1C</lowCelcius>
<highFarenheit>49F</highFarenheit>
<lowFarenheit>31F</lowFarenheit>
<imageURL>rain.gif</imageURL>
</Day4>
<Day5>
<day>Monday</day>
<highCelcius>9C</highCelcius>
<lowCelcius>-4C</lowCelcius>
<highFarenheit>49F</highFarenheit>
<lowFarenheit>24F</lowFarenheit>
<imageURL>snow.gif</imageURL>
</Day5>
</fiveDayForecast>
</CurrentConditions>
</weatherFeed>
<---------CODE--------------->

thanks in advance,
PM
Nov 12 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Hi Paul,

The following is one way (of many ways) you can get at the elements in your
document. I find this way easy to follow and I'm sure there are more
elegant ways of accomplishing the same end result.

1. You can use the xmldocument class to load in your xml document. This
class exists in system in the system.xml namespace.
e.g. Dim myXMLDoc as new xmldocument()

Try
myXMLDoc.load(<path and filename of your xml document>

Catch
....

2. Declare the following xmlelement types
Dim xe_CurrentConditions as XMLElement
Dim xe_CurrentConditionsChild As XMLElement
Dim xe_FiveDayForecast as XMLElement
Dim xe_FiveDayForecastDay as XMLElement
Dim xe_FiveDayForecastDayDetails as XMLElement

3. Set your elements by doing the following

xe_CurrentConditions =
myXMLDoc.SelectSingleNode("/weatherFeed/CurrentConditions") ' The string
in the function is an xpath expression

' this is extremely important to get familiar with when working with

' xml documents ALSO XML and XPath ARE CASE SENSTIVE AS WELL
4. You can cycle through your xe_CurrentConditions by using the following
loop

For Each xe_CurrentConditionsChild in xe_CurrentConditions.childNodes
Dim strThisElementName as String
Dim strThisElementValue as String

strThisElementName = xe_CurrentConditionsChild.Name ' Should
yield country this 1st time thru your loop
strThisElementValue = xe_CurrentConditionsChild.InnerText '
Should Albania 1st time thru
Next

5. You can cycle through your FiveDayForecast by doing the same thing by
using a nested For Each and getting your

xe_FiveDayForecast =
myXMLDoc.SelectSingleNode("weatherFeed/CurrentConditions/fiveDayForecast")

For Each xe_FiveDayForecastDay in xe_xe_FiveDayForecast.ChildNodes
' the elemnt name of xe_FiveDayForecastDay will be Day1 the first
time thru, Day2 the 2nd time thru...
For Each xe_FiveDayForecastDayDetails in
xe_FiveDayForecastDay.ChildNodes
Dim strThisElementName as String
Dim strThisElementValue as String

' This will let you cycle through each <day>
<highCelius><lowCelius> elements You can use them as you see fit.
strThisElementName = xe_CurrentConditionsChild.Name '
element name is
strThisElementValue = xe_CurrentConditionsChild.InnerText
Next
Next

I hope this helps.

Larry R

"Paul M" <mi******@hotmail.com> wrote in message
news:uP**************@tk2msftngp13.phx.gbl...
hi there,

i have an xml file, but am not too sure how to read all these elements using vb.net code.

any help would be greatly appreciated:))
<---------CODE--------------->

<?xml version="1.0" encoding="utf-8"?>
<weatherFeed>
<CurrentConditions>
<country>Albania</country>
<city>Tirana</city>
<weatherImageURL>cloudy.gif</weatherImageURL>
<celcius>14C</celcius>
<farenheit>57F</farenheit>
<description>Partly Cloudy</description>
<relativeHumidity>61%</relativeHumidity>
<wind>WSW at 10 mph (16 km/h)</wind>
<sunrise>7:05 AM</sunrise>
<sunset>4:36 PM</sunset>
<barometricPressure>26.1Hg (F)</barometricPressure>
<lastUpdated>15/01/2004 13:21:14</lastUpdated>
<!--5 Day Weather Forecast-->
<fiveDayForecast>
<Day1>
<day>Thursday</day>
<highCelcius>14C</highCelcius>
<lowCelcius>1C</lowCelcius>
<highFarenheit>58F</highFarenheit>
<lowFarenheit>33F</lowFarenheit>
<imageURL>cloudy.gif</imageURL>
</Day1>
<Day2>
<day>Friday</day>
<highCelcius>11C</highCelcius>
<lowCelcius>1C</lowCelcius>
<highFarenheit>51F</highFarenheit>
<lowFarenheit>34F</lowFarenheit>
<imageURL>cloudy.gif</imageURL>
</Day2>
<Day3>
<day>Saturday</day>
<highCelcius>11C</highCelcius>
<lowCelcius>1C</lowCelcius>
<highFarenheit>51F</highFarenheit>
<lowFarenheit>33F</lowFarenheit>
<imageURL>cloudy.gif</imageURL>
</Day3>
<Day4>
<day>Sunday</day>
<highCelcius>9C</highCelcius>
<lowCelcius>-1C</lowCelcius>
<highFarenheit>49F</highFarenheit>
<lowFarenheit>31F</lowFarenheit>
<imageURL>rain.gif</imageURL>
</Day4>
<Day5>
<day>Monday</day>
<highCelcius>9C</highCelcius>
<lowCelcius>-4C</lowCelcius>
<highFarenheit>49F</highFarenheit>
<lowFarenheit>24F</lowFarenheit>
<imageURL>snow.gif</imageURL>
</Day5>
</fiveDayForecast>
</CurrentConditions>
</weatherFeed>
<---------CODE--------------->

thanks in advance,
PM

Nov 12 '05 #2

P: n/a
thanks heaps!!!

this looks like an excellent example. i'll get right onto it and hope to get
it working soon.

regards,
Paul.

"Larry Rubin" <ru****@aeltus.com> wrote in message
news:eA**************@TK2MSFTNGP11.phx.gbl...
Hi Paul,

The following is one way (of many ways) you can get at the elements in your document. I find this way easy to follow and I'm sure there are more
elegant ways of accomplishing the same end result.

1. You can use the xmldocument class to load in your xml document. This
class exists in system in the system.xml namespace.
e.g. Dim myXMLDoc as new xmldocument()

Try
myXMLDoc.load(<path and filename of your xml document>

Catch
....

2. Declare the following xmlelement types
Dim xe_CurrentConditions as XMLElement
Dim xe_CurrentConditionsChild As XMLElement
Dim xe_FiveDayForecast as XMLElement
Dim xe_FiveDayForecastDay as XMLElement
Dim xe_FiveDayForecastDayDetails as XMLElement

3. Set your elements by doing the following

xe_CurrentConditions =
myXMLDoc.SelectSingleNode("/weatherFeed/CurrentConditions") ' The string in the function is an xpath expression

' this is extremely important to get familiar with when working with

' xml documents ALSO XML and XPath ARE CASE SENSTIVE AS WELL
4. You can cycle through your xe_CurrentConditions by using the following
loop

For Each xe_CurrentConditionsChild in xe_CurrentConditions.childNodes
Dim strThisElementName as String
Dim strThisElementValue as String

strThisElementName = xe_CurrentConditionsChild.Name ' Should
yield country this 1st time thru your loop
strThisElementValue = xe_CurrentConditionsChild.InnerText '
Should Albania 1st time thru
Next

5. You can cycle through your FiveDayForecast by doing the same thing by
using a nested For Each and getting your

xe_FiveDayForecast =
myXMLDoc.SelectSingleNode("weatherFeed/CurrentConditions/fiveDayForecast")

For Each xe_FiveDayForecastDay in xe_xe_FiveDayForecast.ChildNodes
' the elemnt name of xe_FiveDayForecastDay will be Day1 the first time thru, Day2 the 2nd time thru...
For Each xe_FiveDayForecastDayDetails in
xe_FiveDayForecastDay.ChildNodes
Dim strThisElementName as String
Dim strThisElementValue as String

' This will let you cycle through each <day>
<highCelius><lowCelius> elements You can use them as you see fit.
strThisElementName = xe_CurrentConditionsChild.Name '
element name is
strThisElementValue = xe_CurrentConditionsChild.InnerText
Next
Next

I hope this helps.

Larry R

"Paul M" <mi******@hotmail.com> wrote in message
news:uP**************@tk2msftngp13.phx.gbl...
hi there,

i have an xml file, but am not too sure how to read all these elements

using
vb.net code.

any help would be greatly appreciated:))
<---------CODE--------------->

<?xml version="1.0" encoding="utf-8"?>
<weatherFeed>
<CurrentConditions>
<country>Albania</country>
<city>Tirana</city>
<weatherImageURL>cloudy.gif</weatherImageURL>
<celcius>14C</celcius>
<farenheit>57F</farenheit>
<description>Partly Cloudy</description>
<relativeHumidity>61%</relativeHumidity>
<wind>WSW at 10 mph (16 km/h)</wind>
<sunrise>7:05 AM</sunrise>
<sunset>4:36 PM</sunset>
<barometricPressure>26.1Hg (F)</barometricPressure>
<lastUpdated>15/01/2004 13:21:14</lastUpdated>
<!--5 Day Weather Forecast-->
<fiveDayForecast>
<Day1>
<day>Thursday</day>
<highCelcius>14C</highCelcius>
<lowCelcius>1C</lowCelcius>
<highFarenheit>58F</highFarenheit>
<lowFarenheit>33F</lowFarenheit>
<imageURL>cloudy.gif</imageURL>
</Day1>
<Day2>
<day>Friday</day>
<highCelcius>11C</highCelcius>
<lowCelcius>1C</lowCelcius>
<highFarenheit>51F</highFarenheit>
<lowFarenheit>34F</lowFarenheit>
<imageURL>cloudy.gif</imageURL>
</Day2>
<Day3>
<day>Saturday</day>
<highCelcius>11C</highCelcius>
<lowCelcius>1C</lowCelcius>
<highFarenheit>51F</highFarenheit>
<lowFarenheit>33F</lowFarenheit>
<imageURL>cloudy.gif</imageURL>
</Day3>
<Day4>
<day>Sunday</day>
<highCelcius>9C</highCelcius>
<lowCelcius>-1C</lowCelcius>
<highFarenheit>49F</highFarenheit>
<lowFarenheit>31F</lowFarenheit>
<imageURL>rain.gif</imageURL>
</Day4>
<Day5>
<day>Monday</day>
<highCelcius>9C</highCelcius>
<lowCelcius>-4C</lowCelcius>
<highFarenheit>49F</highFarenheit>
<lowFarenheit>24F</lowFarenheit>
<imageURL>snow.gif</imageURL>
</Day5>
</fiveDayForecast>
</CurrentConditions>
</weatherFeed>
<---------CODE--------------->

thanks in advance,
PM


Nov 12 '05 #3

P: n/a
actually, just one more thing.

the CurrentConditions tag, may appear multiple times depending on how many
cities there are for that country.
Is this also easy to implement?

thanks again for your valuable help.

Paul.

"Larry Rubin" <ru****@aeltus.com> wrote in message
news:eA**************@TK2MSFTNGP11.phx.gbl...
Hi Paul,

The following is one way (of many ways) you can get at the elements in your document. I find this way easy to follow and I'm sure there are more
elegant ways of accomplishing the same end result.

1. You can use the xmldocument class to load in your xml document. This
class exists in system in the system.xml namespace.
e.g. Dim myXMLDoc as new xmldocument()

Try
myXMLDoc.load(<path and filename of your xml document>

Catch
....

2. Declare the following xmlelement types
Dim xe_CurrentConditions as XMLElement
Dim xe_CurrentConditionsChild As XMLElement
Dim xe_FiveDayForecast as XMLElement
Dim xe_FiveDayForecastDay as XMLElement
Dim xe_FiveDayForecastDayDetails as XMLElement

3. Set your elements by doing the following

xe_CurrentConditions =
myXMLDoc.SelectSingleNode("/weatherFeed/CurrentConditions") ' The string in the function is an xpath expression

' this is extremely important to get familiar with when working with

' xml documents ALSO XML and XPath ARE CASE SENSTIVE AS WELL
4. You can cycle through your xe_CurrentConditions by using the following
loop

For Each xe_CurrentConditionsChild in xe_CurrentConditions.childNodes
Dim strThisElementName as String
Dim strThisElementValue as String

strThisElementName = xe_CurrentConditionsChild.Name ' Should
yield country this 1st time thru your loop
strThisElementValue = xe_CurrentConditionsChild.InnerText '
Should Albania 1st time thru
Next

5. You can cycle through your FiveDayForecast by doing the same thing by using a nested For Each and getting your

xe_FiveDayForecast =
myXMLDoc.SelectSingleNode("weatherFeed/CurrentConditions/fiveDayForecast")

For Each xe_FiveDayForecastDay in xe_xe_FiveDayForecast.ChildNodes
' the elemnt name of xe_FiveDayForecastDay will be Day1 the first time thru, Day2 the 2nd time thru...
For Each xe_FiveDayForecastDayDetails in
xe_FiveDayForecastDay.ChildNodes
Dim strThisElementName as String
Dim strThisElementValue as String

' This will let you cycle through each <day>
<highCelius><lowCelius> elements You can use them as you see fit.
strThisElementName = xe_CurrentConditionsChild.Name '
element name is
strThisElementValue = xe_CurrentConditionsChild.InnerText
Next
Next

I hope this helps.

Larry R

"Paul M" <mi******@hotmail.com> wrote in message
news:uP**************@tk2msftngp13.phx.gbl...
hi there,

i have an xml file, but am not too sure how to read all these elements

using
vb.net code.

any help would be greatly appreciated:))
<---------CODE--------------->

<?xml version="1.0" encoding="utf-8"?>
<weatherFeed>
<CurrentConditions>
<country>Albania</country>
<city>Tirana</city>
<weatherImageURL>cloudy.gif</weatherImageURL>
<celcius>14C</celcius>
<farenheit>57F</farenheit>
<description>Partly Cloudy</description>
<relativeHumidity>61%</relativeHumidity>
<wind>WSW at 10 mph (16 km/h)</wind>
<sunrise>7:05 AM</sunrise>
<sunset>4:36 PM</sunset>
<barometricPressure>26.1Hg (F)</barometricPressure>
<lastUpdated>15/01/2004 13:21:14</lastUpdated>
<!--5 Day Weather Forecast-->
<fiveDayForecast>
<Day1>
<day>Thursday</day>
<highCelcius>14C</highCelcius>
<lowCelcius>1C</lowCelcius>
<highFarenheit>58F</highFarenheit>
<lowFarenheit>33F</lowFarenheit>
<imageURL>cloudy.gif</imageURL>
</Day1>
<Day2>
<day>Friday</day>
<highCelcius>11C</highCelcius>
<lowCelcius>1C</lowCelcius>
<highFarenheit>51F</highFarenheit>
<lowFarenheit>34F</lowFarenheit>
<imageURL>cloudy.gif</imageURL>
</Day2>
<Day3>
<day>Saturday</day>
<highCelcius>11C</highCelcius>
<lowCelcius>1C</lowCelcius>
<highFarenheit>51F</highFarenheit>
<lowFarenheit>33F</lowFarenheit>
<imageURL>cloudy.gif</imageURL>
</Day3>
<Day4>
<day>Sunday</day>
<highCelcius>9C</highCelcius>
<lowCelcius>-1C</lowCelcius>
<highFarenheit>49F</highFarenheit>
<lowFarenheit>31F</lowFarenheit>
<imageURL>rain.gif</imageURL>
</Day4>
<Day5>
<day>Monday</day>
<highCelcius>9C</highCelcius>
<lowCelcius>-4C</lowCelcius>
<highFarenheit>49F</highFarenheit>
<lowFarenheit>24F</lowFarenheit>
<imageURL>snow.gif</imageURL>
</Day5>
</fiveDayForecast>
</CurrentConditions>
</weatherFeed>
<---------CODE--------------->

thanks in advance,
PM


Nov 12 '05 #4

P: n/a
Paul, I'm glad the example will be of help to you.

In the case of the <CurrentConditions> tag appearing multiple times, you
would establish a For Each loop on a xe_WeatherFeed element.

For example, xe_WeatherFeed = myXMLDoc.SelectSingleNode("/weatherFeed")

' This loop would be the outermost loop in the code example below.
For Each xe_CurrentCondition in xeWeatherFeed.ChildNodes

' The 1st time through would be for your first city, the 2nd time
through would be your 2nd city... etc.

Next

Larry R

"Paul M" <mi******@hotmail.com> wrote in message
news:eX**************@TK2MSFTNGP10.phx.gbl...
actually, just one more thing.

the CurrentConditions tag, may appear multiple times depending on how many
cities there are for that country.
Is this also easy to implement?

thanks again for your valuable help.

Paul.

"Larry Rubin" <ru****@aeltus.com> wrote in message
news:eA**************@TK2MSFTNGP11.phx.gbl...
Hi Paul,

The following is one way (of many ways) you can get at the elements in your
document. I find this way easy to follow and I'm sure there are more
elegant ways of accomplishing the same end result.

1. You can use the xmldocument class to load in your xml document. This class exists in system in the system.xml namespace.
e.g. Dim myXMLDoc as new xmldocument()

Try
myXMLDoc.load(<path and filename of your xml document>

Catch
....

2. Declare the following xmlelement types
Dim xe_CurrentConditions as XMLElement
Dim xe_CurrentConditionsChild As XMLElement
Dim xe_FiveDayForecast as XMLElement
Dim xe_FiveDayForecastDay as XMLElement
Dim xe_FiveDayForecastDayDetails as XMLElement

3. Set your elements by doing the following

xe_CurrentConditions =
myXMLDoc.SelectSingleNode("/weatherFeed/CurrentConditions") ' The

string
in the function is an xpath expression

' this is extremely important to get familiar with when working with

' xml documents ALSO XML and XPath ARE CASE SENSTIVE AS WELL
4. You can cycle through your xe_CurrentConditions by using the following loop

For Each xe_CurrentConditionsChild in xe_CurrentConditions.childNodes
Dim strThisElementName as String
Dim strThisElementValue as String

strThisElementName = xe_CurrentConditionsChild.Name ' Should yield country this 1st time thru your loop
strThisElementValue = xe_CurrentConditionsChild.InnerText ' Should Albania 1st time thru
Next

5. You can cycle through your FiveDayForecast by doing the same thing by
using a nested For Each and getting your

xe_FiveDayForecast =

myXMLDoc.SelectSingleNode("weatherFeed/CurrentConditions/fiveDayForecast")
For Each xe_FiveDayForecastDay in xe_xe_FiveDayForecast.ChildNodes
' the elemnt name of xe_FiveDayForecastDay will be Day1 the

first
time thru, Day2 the 2nd time thru...
For Each xe_FiveDayForecastDayDetails in
xe_FiveDayForecastDay.ChildNodes
Dim strThisElementName as String
Dim strThisElementValue as String

' This will let you cycle through each <day>
<highCelius><lowCelius> elements You can use them as you see fit.
strThisElementName = xe_CurrentConditionsChild.Name '
element name is
strThisElementValue = xe_CurrentConditionsChild.InnerText
Next
Next

I hope this helps.

Larry R

"Paul M" <mi******@hotmail.com> wrote in message
news:uP**************@tk2msftngp13.phx.gbl...
hi there,

i have an xml file, but am not too sure how to read all these elements

using
vb.net code.

any help would be greatly appreciated:))
<---------CODE--------------->

<?xml version="1.0" encoding="utf-8"?>
<weatherFeed>
<CurrentConditions>
<country>Albania</country>
<city>Tirana</city>
<weatherImageURL>cloudy.gif</weatherImageURL>
<celcius>14C</celcius>
<farenheit>57F</farenheit>
<description>Partly Cloudy</description>
<relativeHumidity>61%</relativeHumidity>
<wind>WSW at 10 mph (16 km/h)</wind>
<sunrise>7:05 AM</sunrise>
<sunset>4:36 PM</sunset>
<barometricPressure>26.1Hg (F)</barometricPressure>
<lastUpdated>15/01/2004 13:21:14</lastUpdated>
<!--5 Day Weather Forecast-->
<fiveDayForecast>
<Day1>
<day>Thursday</day>
<highCelcius>14C</highCelcius>
<lowCelcius>1C</lowCelcius>
<highFarenheit>58F</highFarenheit>
<lowFarenheit>33F</lowFarenheit>
<imageURL>cloudy.gif</imageURL>
</Day1>
<Day2>
<day>Friday</day>
<highCelcius>11C</highCelcius>
<lowCelcius>1C</lowCelcius>
<highFarenheit>51F</highFarenheit>
<lowFarenheit>34F</lowFarenheit>
<imageURL>cloudy.gif</imageURL>
</Day2>
<Day3>
<day>Saturday</day>
<highCelcius>11C</highCelcius>
<lowCelcius>1C</lowCelcius>
<highFarenheit>51F</highFarenheit>
<lowFarenheit>33F</lowFarenheit>
<imageURL>cloudy.gif</imageURL>
</Day3>
<Day4>
<day>Sunday</day>
<highCelcius>9C</highCelcius>
<lowCelcius>-1C</lowCelcius>
<highFarenheit>49F</highFarenheit>
<lowFarenheit>31F</lowFarenheit>
<imageURL>rain.gif</imageURL>
</Day4>
<Day5>
<day>Monday</day>
<highCelcius>9C</highCelcius>
<lowCelcius>-4C</lowCelcius>
<highFarenheit>49F</highFarenheit>
<lowFarenheit>24F</lowFarenheit>
<imageURL>snow.gif</imageURL>
</Day5>
</fiveDayForecast>
</CurrentConditions>
</weatherFeed>
<---------CODE--------------->

thanks in advance,
PM



Nov 12 '05 #5

P: n/a
Larry,

i've added the external loop to loop through the cities in the XML file, and
in one example, it finds the nine cities but it displays the first one nine
times. I'm sure its something minor but i just cant seem to figure it out.

here is the XML, and source code below:

--------------------XML---------------------
<?xml version="1.0" encoding="utf-8"?>
<weatherFeed>
<!--Current Weather Conditions - taken from CNN.com Weather website.
(16/01/2004 12:43:26)-->
<CurrentConditions>
<!--URL: http://weather.cnn.com/weather/forecast.jsp?locCode=TNCA-->
<country>Aruba</country>
<city>Oranjestad</city>

<weatherImageURL>http://i.cnn.net/cnn/.element/img/1..../lrg/partly.su
nny.gif</weatherImageURL>
<celcius>25C</celcius>
<farenheit>77F</farenheit>
<description>Partly Sunny</description>
<relativeHumidity>88%</relativeHumidity>
<wind>E at 2 mph (3 km/h)</wind>
<sunrise>7:05 AM</sunrise>
<sunset>6:34 PM</sunset>
<barometricPressure>29.89Hg (F)</barometricPressure>
<lastUpdated>16/01/2004 12:43:38</lastUpdated>
<!--5 Day Weather Forecast-->
<fiveDayForecast>
<Day1>
<day>Friday</day>
<highCelcius>30C</highCelcius>
<lowCelcius>24C</lowCelcius>
<highFarenheit>86F</highFarenheit>
<lowFarenheit>75F</lowFarenheit>

<imageURL>http://i.cnn.net/cnn/.element/img/1.0/weather/med/showers.gif</ima
geURL>
</Day1>
<Day2>
<day>Saturday</day>
<highCelcius>29C</highCelcius>
<lowCelcius>24C</lowCelcius>
<highFarenheit>85F</highFarenheit>
<lowFarenheit>75F</lowFarenheit>

<imageURL>http://i.cnn.net/cnn/.element/img/1....rtly.cloudy.gi
f</imageURL>
</Day2>
<Day3>
<day>Sunday</day>
<highCelcius>29C</highCelcius>
<lowCelcius>24C</lowCelcius>
<highFarenheit>85F</highFarenheit>
<lowFarenheit>75F</lowFarenheit>

<imageURL>http://i.cnn.net/cnn/.element/img/1....rtly.cloudy.gi
f</imageURL>
</Day3>
<Day4>
<day>Monday</day>
<highCelcius>29C</highCelcius>
<lowCelcius>24C</lowCelcius>
<highFarenheit>85F</highFarenheit>
<lowFarenheit>76F</lowFarenheit>

<imageURL>http://i.cnn.net/cnn/.element/img/1....rtly.cloudy.gi
f</imageURL>
</Day4>
<Day5>
<day>Tuesday</day>
<highCelcius>29C</highCelcius>
<lowCelcius>24C</lowCelcius>
<highFarenheit>85F</highFarenheit>
<lowFarenheit>75F</lowFarenheit>

<imageURL>http://i.cnn.net/cnn/.element/img/1....rtly.cloudy.gi
f</imageURL>
</Day5>
</fiveDayForecast>
</CurrentConditions>
<CurrentConditions>
<!--URL: http://weather.cnn.com/weather/forecast.jsp?locCode=NUSN-->
<country>Aruba</country>
<city>St. Nicolaas</city>

<weatherImageURL>http://i.cnn.net/cnn/.element/img/1..../lrg/partly.su
nny.gif</weatherImageURL>
<celcius>25C</celcius>
<farenheit>77F</farenheit>
<description>Partly Sunny</description>
<relativeHumidity>88%</relativeHumidity>
<wind>E at 2 mph (3 km/h)</wind>
<sunrise>7:05 AM</sunrise>
<sunset>6:34 PM</sunset>
<barometricPressure>29.89Hg (F)</barometricPressure>
<lastUpdated>16/01/2004 12:44:10</lastUpdated>
<!--5 Day Weather Forecast-->
<fiveDayForecast>
<Day1>
<day>Friday</day>
<highCelcius>30C</highCelcius>
<lowCelcius>24C</lowCelcius>
<highFarenheit>86F</highFarenheit>
<lowFarenheit>75F</lowFarenheit>

<imageURL>http://i.cnn.net/cnn/.element/img/1.0/weather/med/showers.gif</ima
geURL>
</Day1>
<Day2>
<day>Saturday</day>
<highCelcius>29C</highCelcius>
<lowCelcius>24C</lowCelcius>
<highFarenheit>85F</highFarenheit>
<lowFarenheit>75F</lowFarenheit>

<imageURL>http://i.cnn.net/cnn/.element/img/1....rtly.cloudy.gi
f</imageURL>
</Day2>
<Day3>
<day>Sunday</day>
<highCelcius>29C</highCelcius>
<lowCelcius>24C</lowCelcius>
<highFarenheit>85F</highFarenheit>
<lowFarenheit>75F</lowFarenheit>

<imageURL>http://i.cnn.net/cnn/.element/img/1....rtly.cloudy.gi
f</imageURL>
</Day3>
<Day4>
<day>Monday</day>
<highCelcius>29C</highCelcius>
<lowCelcius>24C</lowCelcius>
<highFarenheit>85F</highFarenheit>
<lowFarenheit>76F</lowFarenheit>

<imageURL>http://i.cnn.net/cnn/.element/img/1....rtly.cloudy.gi
f</imageURL>
</Day4>
<Day5>
<day>Tuesday</day>
<highCelcius>29C</highCelcius>
<lowCelcius>24C</lowCelcius>
<highFarenheit>85F</highFarenheit>
<lowFarenheit>75F</lowFarenheit>

<imageURL>http://i.cnn.net/cnn/.element/img/1....rtly.cloudy.gi
f</imageURL>
</Day5>
</fiveDayForecast>
</CurrentConditions>
</weatherFeed>

----------------SOURCE CODE-----------------
Dim xDoc As New XmlDocument
Dim xe_weatherFeed As XmlElement
Dim xe_city As XmlNode
Dim xe_currentConditions As XmlElement
Dim xe_currentConditionsChild As XmlNode
Dim xe_fiveDayForecast As XmlElement
Dim xe_fiveDayForecastDay As XmlElement
Dim xe_fiveDayForecastDayDetails As XmlNode

Try
Dim a() As String
Dim al As New ArrayList
Dim i As Integer

xDoc.Load(strBaseURL & "weather/" &
Request.QueryString("countryID") & ".xml")
xe_weatherFeed = xDoc.SelectSingleNode("/weatherFeed")
xe_city = xDoc.SelectSingleNode("/weatherFeed/city")
xe_currentConditions =
xDoc.SelectSingleNode("/weatherFeed/city/currentConditions")
xe_fiveDayForecast =
xDoc.SelectSingleNode("/weatherFeed/city/fiveDayForecast")

For Each xe_city In xe_weatherFeed.ChildNodes
If xe_city.NodeType = XmlNodeType.Element Then
ReDim Preserve a(41)
i = 0
For Each xe_currentConditionsChild In
xe_currentConditions.ChildNodes
If xe_currentConditionsChild.NodeType =
XmlNodeType.Element Then
a(i) = xe_currentConditionsChild.InnerText
i += 1
End If
Next

For Each xe_fiveDayForecastDay In xe_fiveDayForecast
For Each xe_fiveDayForecastDayDetails In
xe_fiveDayForecastDay
If xe_fiveDayForecastDayDetails.NodeType =
XmlNodeType.Element Then
a(i) =
xe_fiveDayForecastDayDetails.InnerText
i += 1
End If
Next
Next
al.Add(a)
End If
Next
weatherRepeater.DataSource = al
weatherRepeater.DataBind()
Catch err As Exception
Response.Write("<br><font color=red><b>" & err.Message &
"</b></font>")
Finally
xDoc = Nothing
End Try

thanks again for your help.

regards,
Paul.

"Larry Rubin" <ru****@aeltus.com> wrote in message
news:u0**************@tk2msftngp13.phx.gbl...
Paul, I'm glad the example will be of help to you.

In the case of the <CurrentConditions> tag appearing multiple times, you
would establish a For Each loop on a xe_WeatherFeed element.

For example, xe_WeatherFeed = myXMLDoc.SelectSingleNode("/weatherFeed")

' This loop would be the outermost loop in the code example below.
For Each xe_CurrentCondition in xeWeatherFeed.ChildNodes

' The 1st time through would be for your first city, the 2nd time
through would be your 2nd city... etc.

Next

Larry R

"Paul M" <mi******@hotmail.com> wrote in message
news:eX**************@TK2MSFTNGP10.phx.gbl...
actually, just one more thing.

the CurrentConditions tag, may appear multiple times depending on how many
cities there are for that country.
Is this also easy to implement?

thanks again for your valuable help.

Paul.

"Larry Rubin" <ru****@aeltus.com> wrote in message
news:eA**************@TK2MSFTNGP11.phx.gbl...
Hi Paul,

The following is one way (of many ways) you can get at the elements in

your
document. I find this way easy to follow and I'm sure there are more
elegant ways of accomplishing the same end result.

1. You can use the xmldocument class to load in your xml document.

This class exists in system in the system.xml namespace.
e.g. Dim myXMLDoc as new xmldocument()

Try
myXMLDoc.load(<path and filename of your xml document>

Catch
....

2. Declare the following xmlelement types
Dim xe_CurrentConditions as XMLElement
Dim xe_CurrentConditionsChild As XMLElement
Dim xe_FiveDayForecast as XMLElement
Dim xe_FiveDayForecastDay as XMLElement
Dim xe_FiveDayForecastDayDetails as XMLElement

3. Set your elements by doing the following

xe_CurrentConditions =
myXMLDoc.SelectSingleNode("/weatherFeed/CurrentConditions") ' The

string
in the function is an xpath expression

' this is extremely important to get familiar with when working with

' xml documents ALSO XML and XPath ARE CASE SENSTIVE AS WELL
4. You can cycle through your xe_CurrentConditions by using the following loop

For Each xe_CurrentConditionsChild in xe_CurrentConditions.childNodes
Dim strThisElementName as String
Dim strThisElementValue as String

strThisElementName = xe_CurrentConditionsChild.Name ' Should yield country this 1st time thru your loop
strThisElementValue = xe_CurrentConditionsChild.InnerText ' Should Albania 1st time thru
Next

5. You can cycle through your FiveDayForecast by doing the same thing by
using a nested For Each and getting your

xe_FiveDayForecast =

myXMLDoc.SelectSingleNode("weatherFeed/CurrentConditions/fiveDayForecast")
For Each xe_FiveDayForecastDay in xe_xe_FiveDayForecast.ChildNodes
' the elemnt name of xe_FiveDayForecastDay will be Day1 the

first
time thru, Day2 the 2nd time thru...
For Each xe_FiveDayForecastDayDetails in
xe_FiveDayForecastDay.ChildNodes
Dim strThisElementName as String
Dim strThisElementValue as String

' This will let you cycle through each <day>
<highCelius><lowCelius> elements You can use them as you see fit.
strThisElementName = xe_CurrentConditionsChild.Name '
element name is
strThisElementValue = xe_CurrentConditionsChild.InnerText
Next
Next

I hope this helps.

Larry R

"Paul M" <mi******@hotmail.com> wrote in message
news:uP**************@tk2msftngp13.phx.gbl...
> hi there,
>
> i have an xml file, but am not too sure how to read all these elements using
> vb.net code.
>
> any help would be greatly appreciated:))
> <---------CODE--------------->
>
> <?xml version="1.0" encoding="utf-8"?>
> <weatherFeed>
> <CurrentConditions>
> <country>Albania</country>
> <city>Tirana</city>
> <weatherImageURL>cloudy.gif</weatherImageURL>
> <celcius>14C</celcius>
> <farenheit>57F</farenheit>
> <description>Partly Cloudy</description>
> <relativeHumidity>61%</relativeHumidity>
> <wind>WSW at 10 mph (16 km/h)</wind>
> <sunrise>7:05 AM</sunrise>
> <sunset>4:36 PM</sunset>
> <barometricPressure>26.1Hg (F)</barometricPressure>
> <lastUpdated>15/01/2004 13:21:14</lastUpdated>
> <!--5 Day Weather Forecast-->
> <fiveDayForecast>
> <Day1>
> <day>Thursday</day>
> <highCelcius>14C</highCelcius>
> <lowCelcius>1C</lowCelcius>
> <highFarenheit>58F</highFarenheit>
> <lowFarenheit>33F</lowFarenheit>
> <imageURL>cloudy.gif</imageURL>
> </Day1>
> <Day2>
> <day>Friday</day>
> <highCelcius>11C</highCelcius>
> <lowCelcius>1C</lowCelcius>
> <highFarenheit>51F</highFarenheit>
> <lowFarenheit>34F</lowFarenheit>
> <imageURL>cloudy.gif</imageURL>
> </Day2>
> <Day3>
> <day>Saturday</day>
> <highCelcius>11C</highCelcius>
> <lowCelcius>1C</lowCelcius>
> <highFarenheit>51F</highFarenheit>
> <lowFarenheit>33F</lowFarenheit>
> <imageURL>cloudy.gif</imageURL>
> </Day3>
> <Day4>
> <day>Sunday</day>
> <highCelcius>9C</highCelcius>
> <lowCelcius>-1C</lowCelcius>
> <highFarenheit>49F</highFarenheit>
> <lowFarenheit>31F</lowFarenheit>
> <imageURL>rain.gif</imageURL>
> </Day4>
> <Day5>
> <day>Monday</day>
> <highCelcius>9C</highCelcius>
> <lowCelcius>-4C</lowCelcius>
> <highFarenheit>49F</highFarenheit>
> <lowFarenheit>24F</lowFarenheit>
> <imageURL>snow.gif</imageURL>
> </Day5>
> </fiveDayForecast>
> </CurrentConditions>
> </weatherFeed>
> <---------CODE--------------->
>
> thanks in advance,
> PM
>
>



Nov 12 '05 #6

P: n/a
xe_currentConditions =
xDoc.SelectSingleNode("/weatherFeed/city/currentConditions")

is your problem, you need to create a NodeList for the list of
conditions, then you For Each through the NodeList, extracting every
XmlNode.

xnl_currentConditions =
xDoc.SelectNode("/weatherFeed/city/currentConditions")
foreach (XmlElement xe_condition in xnl_currentConditions)
{
// do stuff here
}
Paul M wrote:
Larry,

i've added the external loop to loop through the cities in the XML file, and
in one example, it finds the nine cities but it displays the first one nine
times. I'm sure its something minor but i just cant seem to figure it out.

here is the XML, and source code below:

--------------------XML---------------------
<?xml version="1.0" encoding="utf-8"?>
<weatherFeed>
<!--Current Weather Conditions - taken from CNN.com Weather website.
(16/01/2004 12:43:26)-->
<CurrentConditions>
<!--URL: http://weather.cnn.com/weather/forecast.jsp?locCode=TNCA-->
<country>Aruba</country>
<city>Oranjestad</city>

<weatherImageURL>http://i.cnn.net/cnn/.element/img/1..../lrg/partly.su
nny.gif</weatherImageURL>
<celcius>25C</celcius>
<farenheit>77F</farenheit>
<description>Partly Sunny</description>
<relativeHumidity>88%</relativeHumidity>
<wind>E at 2 mph (3 km/h)</wind>
<sunrise>7:05 AM</sunrise>
<sunset>6:34 PM</sunset>
<barometricPressure>29.89Hg (F)</barometricPressure>
<lastUpdated>16/01/2004 12:43:38</lastUpdated>
<!--5 Day Weather Forecast-->
<fiveDayForecast>
<Day1>
<day>Friday</day>
<highCelcius>30C</highCelcius>
<lowCelcius>24C</lowCelcius>
<highFarenheit>86F</highFarenheit>
<lowFarenheit>75F</lowFarenheit>

<imageURL>http://i.cnn.net/cnn/.element/img/1.0/weather/med/showers.gif</ima
geURL>
</Day1>
<Day2>
<day>Saturday</day>
<highCelcius>29C</highCelcius>
<lowCelcius>24C</lowCelcius>
<highFarenheit>85F</highFarenheit>
<lowFarenheit>75F</lowFarenheit>

<imageURL>http://i.cnn.net/cnn/.element/img/1....rtly.cloudy.gi
f</imageURL>
</Day2>
<Day3>
<day>Sunday</day>
<highCelcius>29C</highCelcius>
<lowCelcius>24C</lowCelcius>
<highFarenheit>85F</highFarenheit>
<lowFarenheit>75F</lowFarenheit>

<imageURL>http://i.cnn.net/cnn/.element/img/1....rtly.cloudy.gi
f</imageURL>
</Day3>
<Day4>
<day>Monday</day>
<highCelcius>29C</highCelcius>
<lowCelcius>24C</lowCelcius>
<highFarenheit>85F</highFarenheit>
<lowFarenheit>76F</lowFarenheit>

<imageURL>http://i.cnn.net/cnn/.element/img/1....rtly.cloudy.gi
f</imageURL>
</Day4>
<Day5>
<day>Tuesday</day>
<highCelcius>29C</highCelcius>
<lowCelcius>24C</lowCelcius>
<highFarenheit>85F</highFarenheit>
<lowFarenheit>75F</lowFarenheit>

<imageURL>http://i.cnn.net/cnn/.element/img/1....rtly.cloudy.gi
f</imageURL>
</Day5>
</fiveDayForecast>
</CurrentConditions>
<CurrentConditions>
<!--URL: http://weather.cnn.com/weather/forecast.jsp?locCode=NUSN-->
<country>Aruba</country>
<city>St. Nicolaas</city>

<weatherImageURL>http://i.cnn.net/cnn/.element/img/1..../lrg/partly.su
nny.gif</weatherImageURL>
<celcius>25C</celcius>
<farenheit>77F</farenheit>
<description>Partly Sunny</description>
<relativeHumidity>88%</relativeHumidity>
<wind>E at 2 mph (3 km/h)</wind>
<sunrise>7:05 AM</sunrise>
<sunset>6:34 PM</sunset>
<barometricPressure>29.89Hg (F)</barometricPressure>
<lastUpdated>16/01/2004 12:44:10</lastUpdated>
<!--5 Day Weather Forecast-->
<fiveDayForecast>
<Day1>
<day>Friday</day>
<highCelcius>30C</highCelcius>
<lowCelcius>24C</lowCelcius>
<highFarenheit>86F</highFarenheit>
<lowFarenheit>75F</lowFarenheit>

<imageURL>http://i.cnn.net/cnn/.element/img/1.0/weather/med/showers.gif</ima
geURL>
</Day1>
<Day2>
<day>Saturday</day>
<highCelcius>29C</highCelcius>
<lowCelcius>24C</lowCelcius>
<highFarenheit>85F</highFarenheit>
<lowFarenheit>75F</lowFarenheit>

<imageURL>http://i.cnn.net/cnn/.element/img/1....rtly.cloudy.gi
f</imageURL>
</Day2>
<Day3>
<day>Sunday</day>
<highCelcius>29C</highCelcius>
<lowCelcius>24C</lowCelcius>
<highFarenheit>85F</highFarenheit>
<lowFarenheit>75F</lowFarenheit>

<imageURL>http://i.cnn.net/cnn/.element/img/1....rtly.cloudy.gi
f</imageURL>
</Day3>
<Day4>
<day>Monday</day>
<highCelcius>29C</highCelcius>
<lowCelcius>24C</lowCelcius>
<highFarenheit>85F</highFarenheit>
<lowFarenheit>76F</lowFarenheit>

<imageURL>http://i.cnn.net/cnn/.element/img/1....rtly.cloudy.gi
f</imageURL>
</Day4>
<Day5>
<day>Tuesday</day>
<highCelcius>29C</highCelcius>
<lowCelcius>24C</lowCelcius>
<highFarenheit>85F</highFarenheit>
<lowFarenheit>75F</lowFarenheit>

<imageURL>http://i.cnn.net/cnn/.element/img/1....rtly.cloudy.gi
f</imageURL>
</Day5>
</fiveDayForecast>
</CurrentConditions>
</weatherFeed>

----------------SOURCE CODE-----------------
Dim xDoc As New XmlDocument
Dim xe_weatherFeed As XmlElement
Dim xe_city As XmlNode
Dim xe_currentConditions As XmlElement
Dim xe_currentConditionsChild As XmlNode
Dim xe_fiveDayForecast As XmlElement
Dim xe_fiveDayForecastDay As XmlElement
Dim xe_fiveDayForecastDayDetails As XmlNode

Try
Dim a() As String
Dim al As New ArrayList
Dim i As Integer

xDoc.Load(strBaseURL & "weather/" &
Request.QueryString("countryID") & ".xml")
xe_weatherFeed = xDoc.SelectSingleNode("/weatherFeed")
xe_city = xDoc.SelectSingleNode("/weatherFeed/city")
xe_currentConditions =
xDoc.SelectSingleNode("/weatherFeed/city/currentConditions")
xe_fiveDayForecast =
xDoc.SelectSingleNode("/weatherFeed/city/fiveDayForecast")

For Each xe_city In xe_weatherFeed.ChildNodes
If xe_city.NodeType = XmlNodeType.Element Then
ReDim Preserve a(41)
i = 0
For Each xe_currentConditionsChild In
xe_currentConditions.ChildNodes
If xe_currentConditionsChild.NodeType =
XmlNodeType.Element Then
a(i) = xe_currentConditionsChild.InnerText
i += 1
End If
Next

For Each xe_fiveDayForecastDay In xe_fiveDayForecast
For Each xe_fiveDayForecastDayDetails In
xe_fiveDayForecastDay
If xe_fiveDayForecastDayDetails.NodeType =
XmlNodeType.Element Then
a(i) =
xe_fiveDayForecastDayDetails.InnerText
i += 1
End If
Next
Next
al.Add(a)
End If
Next
weatherRepeater.DataSource = al
weatherRepeater.DataBind()
Catch err As Exception
Response.Write("<br><font color=red><b>" & err.Message &
"</b></font>")
Finally
xDoc = Nothing
End Try

thanks again for your help.

regards,
Paul.

"Larry Rubin" <ru****@aeltus.com> wrote in message
news:u0**************@tk2msftngp13.phx.gbl...


Nov 12 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.