XPath related question

  • Thread starter Luis Esteban Valencia Muñoz
  • Start date
L

Luis Esteban Valencia Muñoz

on my page I have a DropDownList and a label. My DropDownList is filled from
an xml file using XPathNavigator. What I want to do is when the user selects
a value from my DropDownList I want a certain node to be selected from an
XPathNodeIterator and I want the value of its attribute displayed in the
Label on my page.

Here is my code:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Dim xpd As XPathDocument = New
XPathDocument(Server.MapPath("weather.xml"))
' Get the associated navigator
Dim xpn As XPathNavigator = xpd.CreateNavigator()

Dim strDate, strLine As String
Dim arrDate() As String
If Not IsPostBack Then
' Retrieve nodes to match the expression
Dim xpni As XPathNodeIterator = xpn.Select("//day")
' And dump the results
ddl1.Items.Clear()
While xpni.MoveNext
ddl1.Items.Add(xpni.Current.GetAttribute("t", String.Empty)
& " " & xpni.Current.GetAttribute("dt", String.Empty))
End While
Else

strLine = Trim(ddl1.SelectedItem.Text)
arrDate = strLine.Split(" ")
strDate = arrDate(1) & " " & arrDate(2)
Dim xpni2 As XPathNodeIterator = xpn.Select("//day[./@dt='" &
strDate & "']")


<!--The problem starts here. I don't seem to be able to retrieve the child
node "part" and it's attribute "p" where "p='d'" from my XPathNodeiterator
xpdi2-->


lblDay.Text =
xpni2.Current.SelectChildren(".//part[./@p='d']").Current.ToString
''lblDay.Text =
xpni2.Current.Select(".//part[./@p='d']").Current.ToString

'lblDay.Text =
xpni2.Current.SelectChildren(".//part[./@p='d']").Current.ToString
<!-- end//-->

End If
End Sub

Here is a copy of my weather.xml file:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<weather ver="2.0">
<dayf>
<lsup>5/13/05 3:09 PM EDT</lsup>
<day d="0" t="Friday" dt="May 13">
<hi>N/A</hi>
<low>50</low>
<sunr>5:47 AM</sunr>
<suns>8:07 PM</suns>
<part p="d">
<icon>44</icon>
<t>N/A</t>
<wind>
<s>N/A</s>
<gust>N/A</gust>
<d>N/A</d>
<t>N/A</t>
</wind>
<bt>N/A</bt>
<ppcp>20</ppcp>
<hmid>N/A</hmid>
</part>
<part p="n">
<icon>33</icon>
<t>Mostly Clear</t>
<wind>
<s>8</s>
<gust>N/A</gust>
<d>183</d>
<t>S</t>
</wind>
<bt>M Clear</bt>
<ppcp>20</ppcp>
<hmid>62</hmid>
</part>
</day>
<day d="1" t="Saturday" dt="May 14">
<hi>81</hi>
<low>65</low>
<sunr>5:47 AM</sunr>
<suns>8:08 PM</suns>
<part p="d">
<icon>37</icon>
<t>Isolated T-Storms</t>
<wind>
<s>11</s>
<gust>N/A</gust>
<d>204</d>
<t>SSW</t>
</wind>
<bt>Iso T-Storms</bt>
<ppcp>30</ppcp>
<hmid>58</hmid>
</part>
<part p="n">
<icon>47</icon>
<t>Scattered T-Storms</t>
<wind>
<s>9</s>
<gust>N/A</gust>
<d>199</d>
<t>SSW</t>
</wind>
<bt>Sct T-Storms</bt>
<ppcp>50</ppcp>
<hmid>72</hmid>
</part>
</day>
<day d="2" t="Sunday" dt="May 15">
<hi>78</hi>
<low>54</low>
<sunr>5:46 AM</sunr>
<suns>8:09 PM</suns>
<part p="d">
<icon>37</icon>
<t>Isolated T-Storms</t>
<wind>
<s>10</s>
<gust>N/A</gust>
<d>254</d>
<t>WSW</t>
</wind>
<bt>Iso T-Storms</bt>
<ppcp>30</ppcp>
<hmid>55</hmid>
</part>
<part p="n">
<icon>11</icon>
<t>Showers</t>
<wind>
<s>3</s>
<gust>N/A</gust>
<d>301</d>
<t>WNW</t>
</wind>
<bt>Showers</bt>
<ppcp>60</ppcp>
<hmid>62</hmid>
</part>
</day>
<day d="3" t="Monday" dt="May 16">
<hi>69</hi>
<low>49</low>
<sunr>5:45 AM</sunr>
<suns>8:10 PM</suns>
<part p="d">
<icon>30</icon>
<t>Partly Cloudy</t>
<wind>
<s>7</s>
<gust>N/A</gust>
<d>9</d>
<t>N</t>
</wind>
<bt>P Cloudy</bt>
<ppcp>20</ppcp>
<hmid>53</hmid>
</part>
<part p="n">
<icon>11</icon>
<t>Showers</t>
<wind>
<s>4</s>
<gust>N/A</gust>
<d>66</d>
<t>ENE</t>
</wind>
<bt>Showers</bt>
<ppcp>30</ppcp>
<hmid>66</hmid>
</part>
</day>
<day d="4" t="Tuesday" dt="May 17">
<hi>67</hi>
<low>47</low>
<sunr>5:44 AM</sunr>
<suns>8:11 PM</suns>
<part p="d">
<icon>30</icon>
<t>Partly Cloudy</t>
<wind>
<s>10</s>
<gust>N/A</gust>
<d>46</d>
<t>NE</t>
</wind>
<bt>P Cloudy</bt>
<ppcp>20</ppcp>
<hmid>55</hmid>
</part>
<part p="n">
<icon>11</icon>
<t>Showers</t>
<wind>
<s>3</s>
<gust>N/A</gust>
<d>30</d>
<t>NNE</t>
</wind>
<bt>Showers</bt>
<ppcp>30</ppcp>
<hmid>60</hmid>
</part>
</day>
</dayf>
</weather>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top