N
N. Demos
Hello,
I'm having problems accessing a complex XML child node (latitude &
longitude), and passing it to a function when the XML file has been read
into a DataSet. Specifically, the returned object from accessing the
'latitude' and 'longitude' nodes is not a DataTable (as specified in the
MSDN). I'm not sure what type is being returned, nor how to go about
finding out. Any pointers would be appreciated. Below is the relevent
Error message, XML, and VB code. Thanks for your time and attention.
Regards,
N. Demos
Error Msg:
------------
Exception Details: System.InvalidCastException: Specified cast is not valid.
XML:
--------
<cities>
<city>
<name>Abidjan</name>
<state>Ivory Coast</state>
<latitude>
<deg>5</deg>
<min>19</min>
<sec>0</sec>
<hemisphere>N</hemisphere>
</latitude>
<longitude>
<deg>4</deg>
<min>2</min>
<sec>0</sec>
<hemisphere>W</hemisphere>
</longitude>
</city>
.....
</cities>
ASP (VB)
--------
Public lstbxCities1 As ListBox
Public lblMon As Label
Public Sub Page_Load(Source As Object, E As EventArgs)
Dim dataSet As DataSet = LoadCityData()
If Not dataSet Is Nothing Then
Dim zRow As DataRow
For Each zRow In dataSet.Tables(0).Rows
Dim lstitmListItem As New ListItem()
lstitmListItem.text = zRow.Item("name") & ", " & zRow.Item("state")
'*** Problem Occurs Here ***
lstitmListItem.value = "latitude:" &
ToDegreeString(zRow.Item("latitude")) & _
"longitude:" & ToDegreeString(zRow.Item("longitude"))
lstbxCities1.Items.Add(lstitmListItem)
Next
lstbxCities1.Height = New Unit(20.0, UnitType.EM)
End If
End Sub
Function ToDegreeString(dtblCoords As DataTable) As String
Dim zRow As DataRow = dtblCoords.Rows(0)
Dim dTotDegrees As Double
Dim dDeg As Double = XmlConvert.ToDouble(zRow.Item("deg"))
Dim dMin As Double = XmlConvert.ToDouble(zRow.Item("min"))
Dim dSec As Double = XmlConvert.ToDouble(zRow.Item("sec"))
dTotDegrees = DMSToDegrees(dDeg, dMin, dSec)
Dim strOutput As String = XmlConvert.ToString(dTotDegrees) & "," &
zRow.Item("hemisphere") & ";"
Return strOutput
End Function
I'm having problems accessing a complex XML child node (latitude &
longitude), and passing it to a function when the XML file has been read
into a DataSet. Specifically, the returned object from accessing the
'latitude' and 'longitude' nodes is not a DataTable (as specified in the
MSDN). I'm not sure what type is being returned, nor how to go about
finding out. Any pointers would be appreciated. Below is the relevent
Error message, XML, and VB code. Thanks for your time and attention.
Regards,
N. Demos
Error Msg:
------------
Exception Details: System.InvalidCastException: Specified cast is not valid.
XML:
--------
<cities>
<city>
<name>Abidjan</name>
<state>Ivory Coast</state>
<latitude>
<deg>5</deg>
<min>19</min>
<sec>0</sec>
<hemisphere>N</hemisphere>
</latitude>
<longitude>
<deg>4</deg>
<min>2</min>
<sec>0</sec>
<hemisphere>W</hemisphere>
</longitude>
</city>
.....
</cities>
ASP (VB)
--------
Public lstbxCities1 As ListBox
Public lblMon As Label
Public Sub Page_Load(Source As Object, E As EventArgs)
Dim dataSet As DataSet = LoadCityData()
If Not dataSet Is Nothing Then
Dim zRow As DataRow
For Each zRow In dataSet.Tables(0).Rows
Dim lstitmListItem As New ListItem()
lstitmListItem.text = zRow.Item("name") & ", " & zRow.Item("state")
'*** Problem Occurs Here ***
lstitmListItem.value = "latitude:" &
ToDegreeString(zRow.Item("latitude")) & _
"longitude:" & ToDegreeString(zRow.Item("longitude"))
lstbxCities1.Items.Add(lstitmListItem)
Next
lstbxCities1.Height = New Unit(20.0, UnitType.EM)
End If
End Sub
Function ToDegreeString(dtblCoords As DataTable) As String
Dim zRow As DataRow = dtblCoords.Rows(0)
Dim dTotDegrees As Double
Dim dDeg As Double = XmlConvert.ToDouble(zRow.Item("deg"))
Dim dMin As Double = XmlConvert.ToDouble(zRow.Item("min"))
Dim dSec As Double = XmlConvert.ToDouble(zRow.Item("sec"))
dTotDegrees = DMSToDegrees(dDeg, dMin, dSec)
Dim strOutput As String = XmlConvert.ToString(dTotDegrees) & "," &
zRow.Item("hemisphere") & ";"
Return strOutput
End Function