Click here to Skip to main content
15,917,709 members
Home / Discussions / XML / XSL
   

XML / XSL

 
Questionremove periculat node from xml file Pin
rkhokle22-Aug-06 20:57
rkhokle22-Aug-06 20:57 
AnswerRe: remove periculat node from xml file Pin
Christian Graus22-Aug-06 21:07
protectorChristian Graus22-Aug-06 21:07 
QuestionSaving svg or xml to client side Pin
Yeast2722-Aug-06 7:31
Yeast2722-Aug-06 7:31 
Questionxml Pin
rkhokale19-Aug-06 3:04
rkhokale19-Aug-06 3:04 
AnswerRe: xml Pin
Vitaliy Tsvayer20-Aug-06 9:09
Vitaliy Tsvayer20-Aug-06 9:09 
AnswerRe: xml Pin
Dustin Metzgar22-Aug-06 11:15
Dustin Metzgar22-Aug-06 11:15 
AnswerRe: xml Pin
Sage30-Aug-06 15:04
Sage30-Aug-06 15:04 
QuestionI'm Sure It's Something Little... Pin
Scott_Roberts18-Aug-06 15:15
Scott_Roberts18-Aug-06 15:15 
...which is why I'm pasting in 300 lines of code OMG | :OMG:

The ParseDaysXML routine is based on the ParseWeatherXML Routine. Not surprisingly, the code I copeid and pasted is working fine, but the stuff I tried to adapt isn't.

The commented code in ParseDaysXML is what is causing the problem. Based on the "Object reference not set to an instance of an object" error pointing to line 123 (second Dim line in the FindLayoutTable function), I'm suspecting something isn't right with my SelectSingleNode line (the first commented one).

So, three questions:
1) What is causing the error
2) how to fix?
3) My goal is simply to get period name from the WKKI2.xml file matched with the appopriate period in thew WKKI.xml file. Is there a more efficient way to go about it?

Thanks in advance....
Scott

---------------------------
Imports System.Xml
Imports System.Text
Imports System.Math

Public Class NWS_XML
#Region "Structs"

Private strStation As String

Private Structure WeatherTable
Public nodTimeLayout As XmlNode
Public nodData As XmlNode
End Structure

Private Structure WeatherData
Public wtHighTemp As WeatherTable
Public wtLowTemp As WeatherTable
Public wtLiquidPrecip As WeatherTable
Public wtSnowPrecip As WeatherTable
Public wtPrecipProb As WeatherTable
Public wtWindSpeed As WeatherTable
Public wtWindDirection As WeatherTable
Public wtCloudCover As WeatherTable
Public wtDayName As WeatherTable
Public wtNightName As WeatherTable
End Structure
Private Structure DayData
Public PeriodName As String
Public dt As Date
End Structure

Private Structure TemperatureData
Public F As Integer
Public ReadOnly Property C() As Integer
Get
Return Integer.Parse(Math.Round(((Double.Parse(F) - 32) / 9) * 5))
End Get
End Property
Public dt As Date
End Structure

Private Structure PrecipitationData
Public Inches As Double
Public ReadOnly Property MM() As Double
Get
Return Inches * 25.4
End Get
End Property
Public dt As Date
End Structure

Private Structure PercentData
Public Pct As Integer
Public dt As Date
End Structure
Private Structure WindData
Public Knots As Integer
Public ReadOnly Property MPH() As Integer
Get
Return Integer.Parse(Math.Round((Double.Parse(Knots) * 6076.12) / 5280))
End Get
End Property
Public Degrees As Integer
Public ReadOnly Property Dir() As String
Get
Select Case Degrees
Case 0 To 11.25, 348.75 To 360
Return "N"
Case 11.25 To 33.75
Return "NNE"
Case 33.75 To 56.25
Return "NE"
Case 56.25 To 78.75
Return "ENE"
Case 78.75 To 101.25
Return "E"
Case 101.25 To 123.75
Return "ESE"
Case 123.75 To 146.25
Return "SE"
Case 146.25 To 168.75
Return "SSE"
Case 168.75 To 191.25
Return "S"
Case 191.25 To 213.75
Return "SSW"
Case 213.75 To 236.25
Return "SW"
Case 236.25 To 258.75
Return "WSW"
Case 258.75 To 281.25
Return "W"
Case 281.25 To 303.75
Return "WNW"
Case 303.75 To 326.25
Return "NW"
Case 326.25 To 348.75
Return "NNW"
End Select
End Get
End Property
Public dt As Date
End Structure

Private Structure FormattedWeatherData
Public HighTemp() As TemperatureData
Public LowTemp() As TemperatureData
Public LiquidPrecip() As PrecipitationData
Public SnowPrecip() As PrecipitationData
Public PrecipProb() As PercentData
Public Wind() As WindData
Public CloudCover() As PercentData
Public DayName() As DayData
Public NightName() As DayData
End Structure

#End Region

#Region "NWS XML Reader http://www.nws.noaa.gov/forecasts/xml"

Private Function FindLayoutTable(ByVal xmlDoc As XmlDocument, ByVal nodData As XmlNode)
Dim nlTimeLayouts As XmlNodeList = xmlDoc.SelectNodes("/dwml/data/time-layout")
Dim strTimeLayout As String = nodData.Attributes("time-layout").Value

Dim node As XmlNode
For Each node In nlTimeLayouts
If strTimeLayout = node.SelectSingleNode("layout-key").InnerText Then
Return node
End If
Next

Return Nothing
End Function

Private Function ParseDateTime(ByVal str As String) As Date
Return Date.Parse(str.Replace("T", " ").Substring(0, str.LastIndexOf("-")))
End Function

Private Sub FillTemperatureData(ByVal wt As WeatherTable, ByRef temp() As TemperatureData)
Dim intCount As Integer

Dim nlData As XmlNodeList = wt.nodData.SelectNodes("value")
Dim nlDate As XmlNodeList = wt.nodTimeLayout.SelectNodes("start-valid-time")
For intCount = 0 To UBound(temp) - 1
temp(intCount) = New TemperatureData()
temp(intCount).F = Integer.Parse(nlData(intCount).InnerText)
temp(intCount).dt = ParseDateTime(nlDate(intCount).InnerText)
Next
End Sub

Private Sub FillPrecipitationData(ByVal wt As WeatherTable, ByRef precip() As PrecipitationData)
Dim intCount As Integer

Dim nlData As XmlNodeList = wt.nodData.SelectNodes("value")
Dim nlDate As XmlNodeList = wt.nodTimeLayout.SelectNodes("start-valid-time")
For intCount = 0 To UBound(precip) - 1
precip(intCount) = New PrecipitationData()
If Len(nlData(intCount).InnerText) = 0 Then
precip(intCount).Inches = 0
Else
precip(intCount).Inches = Double.Parse(nlData(intCount).InnerText)
End If
precip(intCount).dt = ParseDateTime(nlDate(intCount).InnerText)
Next
End Sub

Private Sub FillPercentData(ByVal wt As WeatherTable, ByRef pct() As PercentData)
Dim intCount As Integer

Dim nlData As XmlNodeList = wt.nodData.SelectNodes("value")
Dim nlDate As XmlNodeList = wt.nodTimeLayout.SelectNodes("start-valid-time")
For intCount = 0 To UBound(pct) - 1
pct(intCount) = New PercentData()
pct(intCount).Pct = Integer.Parse(nlData(intCount).InnerText)
pct(intCount).dt = ParseDateTime(nlDate(intCount).InnerText)
Next
End Sub

Private Sub FillWindData(ByVal wtSpeed As WeatherTable, ByVal wtDir As WeatherTable, ByRef wind() As WindData)
Dim intCount As Integer

Dim nlSpeed As XmlNodeList = wtSpeed.nodData.SelectNodes("value")
Dim nlDir As XmlNodeList = wtDir.nodData.SelectNodes("value")
Dim nlDate As XmlNodeList = wtSpeed.nodTimeLayout.SelectNodes("start-valid-time")
For intCount = 0 To UBound(wind) - 1
wind(intCount) = New WindData()
wind(intCount).Knots = Integer.Parse(nlSpeed(intCount).InnerText)
wind(intCount).Degrees = Integer.Parse(nlDir(intCount).InnerText)
wind(intCount).dt = ParseDateTime(nlDate(intCount).InnerText)
Next
End Sub

Private Sub FillDayData(ByVal wt As WeatherTable, ByRef DName() As DayData)
Dim intCount As Integer

Dim nlData As XmlNodeList = wt.nodData.SelectNodes("value")
Dim nlDate As XmlNodeList = wt.nodTimeLayout.SelectNodes("period-name")
For intCount = 0 To UBound(DName) - 1
DName(intCount) = New DayData()
DName(intCount).PeriodName = nlData(intCount).InnerText
DName(intCount).dt = ParseDateTime(nlDate(intCount).InnerText)
Next
End Sub

Private Function ParseForecastXML(ByVal strXMLWeather) As FormattedWeatherData
Try
'Setup variables
Dim xmlDoc As New XmlDocument()
Dim wdData As New WeatherData()
Dim fwdData As New FormattedWeatherData()
wdData.wtHighTemp = New WeatherTable()
wdData.wtLowTemp = New WeatherTable()
wdData.wtLiquidPrecip = New WeatherTable()
wdData.wtSnowPrecip = New WeatherTable()
wdData.wtPrecipProb = New WeatherTable()
wdData.wtWindSpeed = New WeatherTable()
wdData.wtWindDirection = New WeatherTable()
wdData.wtCloudCover = New WeatherTable()

'Load XML data
xmlDoc.Load(strXMLWeather)

'Load data and their corresponding time nodes
wdData.wtHighTemp.nodData = xmlDoc.SelectSingleNode("/dwml/data/parameters/temperature[@type='maximum']")
wdData.wtLowTemp.nodData = xmlDoc.SelectSingleNode("/dwml/data/parameters/temperature[@type='minimum']")
wdData.wtLiquidPrecip.nodData = xmlDoc.SelectSingleNode("/dwml/data/parameters/precipitation[@type='liquid']")
wdData.wtSnowPrecip.nodData = xmlDoc.SelectSingleNode("/dwml/data/parameters/precipitation[@type='snow']")
wdData.wtPrecipProb.nodData = xmlDoc.SelectSingleNode("/dwml/data/parameters/probability-of-precipitation[@type='12 hour']")
wdData.wtWindSpeed.nodData = xmlDoc.SelectSingleNode("/dwml/data/parameters/wind-speed[@type='sustained']")
wdData.wtWindDirection.nodData = xmlDoc.SelectSingleNode("/dwml/data/parameters/direction[@type='wind']")
wdData.wtCloudCover.nodData = xmlDoc.SelectSingleNode("/dwml/data/parameters/cloud-amount[@type='total']")

wdData.wtHighTemp.nodTimeLayout = FindLayoutTable(xmlDoc, wdData.wtHighTemp.nodData)
wdData.wtLowTemp.nodTimeLayout = FindLayoutTable(xmlDoc, wdData.wtLowTemp.nodData)
wdData.wtLiquidPrecip.nodTimeLayout = FindLayoutTable(xmlDoc, wdData.wtLiquidPrecip.nodData)
wdData.wtSnowPrecip.nodTimeLayout = FindLayoutTable(xmlDoc, wdData.wtSnowPrecip.nodData)
wdData.wtPrecipProb.nodTimeLayout = FindLayoutTable(xmlDoc, wdData.wtPrecipProb.nodData)
wdData.wtWindSpeed.nodTimeLayout = FindLayoutTable(xmlDoc, wdData.wtWindSpeed.nodData)
wdData.wtWindDirection.nodTimeLayout = FindLayoutTable(xmlDoc, wdData.wtWindDirection.nodData)
wdData.wtCloudCover.nodTimeLayout = FindLayoutTable(xmlDoc, wdData.wtCloudCover.nodData)

'Setup formatted data variables
ReDim fwdData.HighTemp(wdData.wtHighTemp.nodTimeLayout.SelectNodes("start-valid-time").Count)
ReDim fwdData.LowTemp(wdData.wtLowTemp.nodTimeLayout.SelectNodes("start-valid-time").Count)
ReDim fwdData.LiquidPrecip(wdData.wtLiquidPrecip.nodTimeLayout.SelectNodes("start-valid-time").Count)
ReDim fwdData.SnowPrecip(wdData.wtSnowPrecip.nodTimeLayout.SelectNodes("start-valid-time").Count)
ReDim fwdData.PrecipProb(wdData.wtPrecipProb.nodTimeLayout.SelectNodes("start-valid-time").Count)
ReDim fwdData.Wind(wdData.wtWindSpeed.nodTimeLayout.SelectNodes("start-valid-time").Count)
ReDim fwdData.CloudCover(wdData.wtCloudCover.nodTimeLayout.SelectNodes("start-valid-time").Count)

'Fill in data
FillTemperatureData(wdData.wtHighTemp, fwdData.HighTemp)
FillTemperatureData(wdData.wtLowTemp, fwdData.LowTemp)
FillPrecipitationData(wdData.wtLiquidPrecip, fwdData.LiquidPrecip)
FillPrecipitationData(wdData.wtSnowPrecip, fwdData.SnowPrecip)
FillPercentData(wdData.wtPrecipProb, fwdData.PrecipProb)
FillWindData(wdData.wtWindSpeed, wdData.wtWindDirection, fwdData.Wind)
FillPercentData(wdData.wtCloudCover, fwdData.CloudCover)

Return fwdData

Catch ex As Exception
MsgBox(ex.ToString)
Return Nothing
End Try
End Function
Private Function ParseDaysXML(ByVal strXMLWeather) As FormattedWeatherData
Try
'Setup variables
Dim xmlDaysDoc As New XmlDocument()
Dim wdDaysData As New WeatherData()
Dim fwdDaysData As New FormattedWeatherData()

'Load XML data
xmlDaysDoc.Load(strXMLWeather)

'Load data and their corresponding time nodes
'wdDaysData.wtDayName.nodData = xmlDaysDoc.SelectSingleNode("/dwml/data/time-layout/start-valid-time/@period-name")
'wdDaysData.wtNightName.nodData = xmlDaysDoc.SelectSingleNode("/dwml/data/time-layout/start-valid-time/@period-name")
wdDaysData.wtHighTemp.nodData = xmlDaysDoc.SelectSingleNode("/dwml/data/parameters/temperature[@type='maximum']")
wdDaysData.wtLowTemp.nodData = xmlDaysDoc.SelectSingleNode("/dwml/data/parameters/temperature[@type='minimum']")

'wdDaysData.wtDayName.nodTimeLayout = FindLayoutTable(xmlDaysDoc, wdDaysData.wtDayName.nodData)
'wdDaysData.wtNightName.nodTimeLayout = FindLayoutTable(xmlDaysDoc, wdDaysData.wtNightName.nodData)
wdDaysData.wtHighTemp.nodTimeLayout = FindLayoutTable(xmlDaysDoc, wdDaysData.wtHighTemp.nodData)
wdDaysData.wtLowTemp.nodTimeLayout = FindLayoutTable(xmlDaysDoc, wdDaysData.wtLowTemp.nodData)

'Setup formatted data variables
'ReDim fwdDaysData.DayName(wdDaysData.wtDayName.nodTimeLayout.SelectNodes("start-valid-time").Count)
'ReDim fwdDaysData.NightName(wdDaysData.wtNightName.nodTimeLayout.SelectNodes("start-valid-time").Count)
ReDim fwdDaysData.HighTemp(wdDaysData.wtHighTemp.nodTimeLayout.SelectNodes("start-valid-time").Count)
ReDim fwdDaysData.LowTemp(wdDaysData.wtLowTemp.nodTimeLayout.SelectNodes("start-valid-time").Count)

'Fill in data
'FillDayData(wdDaysData.wtHighTemp, fwdDaysData.DayName)
'FillDayData(wdDaysData.wtLowTemp, fwdDaysData.NightName)
FillTemperatureData(wdDaysData.wtHighTemp, fwdDaysData.HighTemp)
FillTemperatureData(wdDaysData.wtLowTemp, fwdDaysData.LowTemp)

Return fwdDaysData

Catch ex As Exception
MsgBox(ex.ToString)
Return Nothing
End Try
End Function

#End Region



Private Sub btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles btnWKKI.Click, btnKMAV.Click, btnWWWC.Click, btnKCHQ.Click

strStation = sender.Text

'load the weather array
Dim strWeatherXML As String = "http://distribution.customradionews.net/Forecast_XML/" & strStation & ".xml"
Dim fwdWeather As FormattedWeatherData = ParseForecastXML(strWeatherXML)

'load the days array
Dim strWeatherDaysXML As String = "http://distribution.customradionews.net/Forecast_XML/" & strStation & "2.xml"
Dim fwdWeatherDays As FormattedWeatherData = ParseDaysXML(strWeatherDaysXML)


QuestionSpecial characters - [modified] Pin
VK-Cadec18-Aug-06 7:28
VK-Cadec18-Aug-06 7:28 
QuestionHow to get popup onTrayIcon Pin
muravi17-Aug-06 22:39
muravi17-Aug-06 22:39 
AnswerRe: How to get popup onTrayIcon Pin
toxcct18-Aug-06 0:24
toxcct18-Aug-06 0:24 
QuestionURGENT innertext problem Pin
biaali17-Aug-06 21:19
biaali17-Aug-06 21:19 
AnswerRe: URGENT innertext problem Pin
Dustin Metzgar18-Aug-06 5:13
Dustin Metzgar18-Aug-06 5:13 
Generalinnertext problem Pin
biaali18-Aug-06 22:51
biaali18-Aug-06 22:51 
QuestionOught to be basic, but I'm not getting it! Pin
Scott_Roberts17-Aug-06 8:13
Scott_Roberts17-Aug-06 8:13 
AnswerRe: Ought to be basic, but I'm not getting it! Pin
led mike17-Aug-06 9:06
led mike17-Aug-06 9:06 
GeneralRe: Ought to be basic, but I'm not getting it! Pin
Scott_Roberts17-Aug-06 10:01
Scott_Roberts17-Aug-06 10:01 
GeneralRe: Ought to be basic, but I'm not getting it! Pin
led mike17-Aug-06 10:53
led mike17-Aug-06 10:53 
GeneralRe: Ought to be basic, but I'm not getting it! Pin
Scott_Roberts17-Aug-06 12:46
Scott_Roberts17-Aug-06 12:46 
GeneralRe: Ought to be basic, but I'm not getting it! Pin
Scott_Roberts17-Aug-06 13:17
Scott_Roberts17-Aug-06 13:17 
GeneralRe: Ought to be basic, but I'm not getting it! Pin
Scott_Roberts17-Aug-06 16:17
Scott_Roberts17-Aug-06 16:17 
QuestionPosition in a node set Pin
whozurmommy16-Aug-06 21:11
whozurmommy16-Aug-06 21:11 
AnswerRe: Position in a node set Pin
Dustin Metzgar17-Aug-06 5:51
Dustin Metzgar17-Aug-06 5:51 
GeneralRe: Position in a node set Pin
whozurmommy17-Aug-06 8:41
whozurmommy17-Aug-06 8:41 
GeneralRe: Position in a node set Pin
Dustin Metzgar17-Aug-06 9:18
Dustin Metzgar17-Aug-06 9:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.