Page 2 of 2 FirstFirst 12
Results 41 to 44 of 44

Thread: Reading from XML

  1. #41

    Thread Starter
    Lively Member
    Join Date
    Sep 2012
    Posts
    101

    Re: Reading from XML

    How can I then solve this problem? I need a method to detect whether I should get the time of Morning, from the currentDay or tomorrow.

  2. #42

    Thread Starter
    Lively Member
    Join Date
    Sep 2012
    Posts
    101

    Re: Reading from XML

    Anyone?

  3. #43
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: Reading from XML

    Hey, Crystalii,

    Consider this line form your CheckStatus method:
    Code:
     Dim CurrentPrayer As String = allTimes.LastOrDefault(Function(kvp) kvp.Value < CurrentTime).Key
    Firstly, should the prayer start AT the time stored in your XML data, as opposed to AFTER that time, you might want to change it to:
    Code:
     Dim CurrentPrayer As String = allTimes.LastOrDefault(Function(kvp) kvp.Value <= CurrentTime).Key
    Secondly, the value returned for CurrentPrayer by that code will be NOTHING when the current time is less than the time given by Morning.

    So if CurrentPrayer = Nothing, then the current Prayer has to be the Prayer started LAST NIGHT.

    Conversely, if CurrentPrayer = "Night" then the next Prayer has to be TOMORROW MORNING.

    These facts lead to the following code:
    vb.net Code:
    1. Public Sub CheckStatus()
    2.     Dim MorningTime As DateTime = Convert.ToDateTime(Morning)
    3.     Dim NoonTime As DateTime = Convert.ToDateTime(Noon)
    4.     Dim AfterNoonTime As DateTime = Convert.ToDateTime(Afternoon)
    5.     Dim SunSetTime As DateTime = Convert.ToDateTime(Sunset)
    6.     Dim NightTime As DateTime = Convert.ToDateTime(Night)
    7.  
    8.     Dim allTimes As New Dictionary(Of String, DateTime) From {{"Morning", MorningTime}, _
    9.                                                               {"Noon", NoonTime}, _
    10.                                                               {"Afternoon", AfterNoonTime}, _
    11.                                                               {"Sunset", SunSetTime}, _
    12.                                                               {"Night", NightTime}}
    13.     Dim CurrentTime As DateTime = DateTime.Now
    14.  
    15.     Dim CurrentPrayer As String = allTimes.LastOrDefault(Function(kvp) kvp.Value <= CurrentTime).Key
    16.  
    17.     '  CurrentPrayer will be NOTHING for times < Morning
    18.     '    so the actual Prayer will be Yesterday's NightTime Prayer
    19.     '    and the next Prayer has to be Today's Morning Prayer
    20.  
    21.     If CurrentPrayer = Nothing Then
    22.         CurrentPrayer = "Yesterday Night"
    23.     End If
    24.  
    25.  
    26.     lblCurrent.Text = "Current prayer: " & CurrentPrayer
    27.  
    28.     If CurrentPrayer = "Yesterday Night" Then
    29.         lblNext.Text = "Next prayer: Morning " & Morning
    30.  
    31.     ElseIf CurrentPrayer = "Morning" Then
    32.         lblNext.Text = "Next prayer: Noon " & Noon
    33.     ElseIf CurrentPrayer = "Noon" Then
    34.         lblNext.Text = "Next prayer: Afternoon " & Afternoon
    35.     ElseIf CurrentPrayer = "Afternoon" Then
    36.         lblNext.Text = "Next prayer: Sunset " & Sunset
    37.     ElseIf CurrentPrayer = "Sunset" Then
    38.         lblNext.Text = "Next prayer: Night " & Night
    39.  
    40.     ElseIf CurrentPrayer = "Night" Then
    41.         '  next Prayer has to be Tomorrow's Morning Prayer
    42.         Dim Tomorrow As DateTime = DateTime.Today.AddDays(1)
    43.         lblNext.Text = "Next prayer: Tomorrow Morning " & GetTime("_" & Tomorrow.ToString("MM"), "_" & Tomorrow.ToString("dd"), "Morning")
    44.     End If
    45. End Sub


    I would also change your GetTime function to allow for the possibility that your XML data file does not contain data for the following day. Something along the lines of:
    vb.net Code:
    1. Private Function GetTime(ByVal monthString As String, ByVal dayString As String, ByVal time As String) As String
    2.     Dim xml As XDocument
    3.  
    4.     xml = XDocument.Load(Application.StartupPath & "\data.xml")
    5.  
    6.     Dim tempTime As String = ""
    7.  
    8.     Dim month As XElement = xml.Root.Element(monthString)
    9.     If month IsNot Nothing Then
    10.         Dim day As XElement = month.Element(dayString)
    11.         If day IsNot Nothing AndAlso day.Element(time) IsNot Nothing Then
    12.             tempTime = day.Element(time).Value
    13.         End If
    14.     End If
    15.  
    16.     Return tempTime
    17. End Function

  4. #44

    Thread Starter
    Lively Member
    Join Date
    Sep 2012
    Posts
    101

    Re: Reading from XML

    Thank you, you fixed one of my biggest problems! It's actually fascinating that the solution was in front of my eyes. But I had been trying so many ways to fix this, and the whole code was such a mess, that I did not know what to do.

    Thank you

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width