XML Navigation
-
Thursday, August 23, 2012 12:18 PM
I am trying to navigate through an xml document which i have called from an RSS Feed link.
Below are the extract codes:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim doc As New System.Xml.XmlDocument
doc.Load("http://earthquake.usgs.gov/earthquakes/shakemap/rss.xml")
Dim list = doc.GetElementsByTagName("item")
Dim i As Integer = 0
For Each item As System.Xml.XmlElement In list
Label1.Text = (doc.GetElementsByTagName("title").Item(i).InnerText)
Label2.Text = (doc.GetElementsByTagName("description").Item(i).InnerText)
Label3.Text = (doc.GetElementsByTagName("link").Item(i).InnerText)
Label4.Text = (doc.GetElementsByTagName("pubDate").Item(i).InnerText)
Label5.Text = (doc.GetElementsByTagName("geo:lat").Item(i).InnerText)
Label6.Text = (doc.GetElementsByTagName("geo:long").Item(i).InnerText)
Label7.Text = (doc.GetElementsByTagName("dc:subject").Item(i).InnerText)
Label8.Text = (doc.GetElementsByTagName("eq:seconds").Item(i).InnerText)
Label9.Text = (doc.GetElementsByTagName("eq:depth").Item(i).InnerText)
Label10.Text = (doc.GetElementsByTagName("eq:region").Item(i).InnerText)
i = i + 1
Next
End SubHowever, I wish to navigate through the xml document by adding a previous, next, first and last buttons. Any ideas would be helpful
All Replies
-
Thursday, August 23, 2012 7:28 PM
You've asked this in a forum which doesn't really deal with ASP.NET (see http://forums.asp.net for that), but if I show you a way with a Windows forms program hopefully you can convert it ASP.NET by saving the currIndex and shakes variables in session variables. And implement the first and last buttons yourself.
Public Class Form1 Dim shakes As New List(Of Shaken) Dim currIndex As Integer = 0 Class Shaken Property Title As String Property Description As String Property Link As Uri Property PubDate As DateTime Property Latitude As Double Property Longitude As Double Property Subject As String Property Duration As Double Property Depth As Double Property Region As String End Class Sub GetData() shakes.Clear() ' chances are the data are in US format Dim locale = Globalization.CultureInfo.CreateSpecificCulture("en-US") Dim doc As New System.Xml.XmlDocument doc.Load("http://earthquake.usgs.gov/earthquakes/shakemap/rss.xml") Dim list = doc.GetElementsByTagName("item") Dim i As Integer = 0 For Each item As System.Xml.XmlElement In list shakes.Add(New Shaken With { .Title = doc.GetElementsByTagName("title").Item(i).InnerText, .Description = doc.GetElementsByTagName("description").Item(i).InnerText, .Link = New Uri(doc.GetElementsByTagName("link").Item(i).InnerText), .PubDate = DateTime.Parse(doc.GetElementsByTagName("pubDate").Item(i).InnerText, locale), .Latitude = CDbl(doc.GetElementsByTagName("geo:lat").Item(i).InnerText), .Longitude = CDbl(doc.GetElementsByTagName("geo:long").Item(i).InnerText), .Subject = doc.GetElementsByTagName("dc:subject").Item(i).InnerText, .Duration = CDbl(doc.GetElementsByTagName("eq:seconds").Item(i).InnerText), .Depth = CDbl(doc.GetElementsByTagName("eq:depth").Item(i).InnerText), .Region = doc.GetElementsByTagName("eq:region").Item(i).InnerText }) i += 1 Next ' order them by PubDate for neatness shakes = shakes.OrderBy(Function(s) s.PubDate).ToList End Sub Sub ShowDatum(n As Integer) lblTitle.Text = shakes(n).Title lblDescription.Text = shakes(n).Description lblLink.Text = shakes(n).Link.ToString lblPubDate.Text = shakes(n).PubDate.ToString("yyyy-MM-dd") lblLatitude.Text = shakes(n).Latitude.ToString lblLongitude.Text = shakes(n).Longitude.ToString lblSubject.Text = shakes(n).Subject lblDuration.Text = shakes(n).Duration.ToString lblDepth.Text = shakes(n).Depth.ToString lblRegion.Text = shakes(n).Region End Sub Private Sub bnPrevious_Click(sender As Object, e As EventArgs) Handles bnPrevious.Click If currIndex > 0 Then currIndex -= 1 ShowDatum(currIndex) If currIndex = 0 Then bnPrevious.Enabled = False End If lblCounter.Text = String.Format("{0} of {1}", currIndex + 1, shakes.Count) End If bnNext.Enabled = True End Sub Private Sub bnNext_Click(sender As Object, e As EventArgs) Handles bnNext.Click If currIndex < shakes.Count - 1 Then currIndex += 1 ShowDatum(currIndex) If currIndex = shakes.Count - 1 Then bnNext.Enabled = False End If lblCounter.Text = String.Format("{0} of {1}", currIndex + 1, shakes.Count) End If bnPrevious.Enabled = True End Sub Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load GetData() currIndex = 0 bnPrevious.Enabled = False lblCounter.Text = String.Format("{0} of {1}", currIndex + 1, shakes.Count) ShowDatum(currIndex) End Sub End ClassI gave the labels more descriptive terms, and added a label for the current value of the counter.
HTH,
Andrew
- Marked As Answer by Darkminded Elements Friday, August 24, 2012 3:36 AM
-
Thursday, August 23, 2012 7:32 PM
Oh yeah, forgot to mention: the description field doesn't seem to be right. But whatever it is appears to have a URL for an image which you might want to put on the page too.
--
Andrew -
Friday, August 24, 2012 3:37 AM
Thanks Andrew, I'm new to this that's why got in the wrong forum.
Thanks Mate.

