|
-
Mar 28th, 2013, 10:06 AM
#1
Thread Starter
Lively Member
Reading from XML
Hello!
I am making some sort of alarm program. The alarm goes on 5 times a day, at certain times. The times differs for each day. For example, today the alarm will go on at these times:
Morning: 04:04
Noon: 12:05
Afternoon: 15:22
Sunset: 18:26
Night: 19:57
And tomorrow the time would be:
Morning: 04:01
Noon: 12:05
Afternoon: 15:24
Sunset: 18:29
Night: 20:00
As you see, the times are different for each day. There is 365 days in a year, so that would mean 1875 different times, and each time is unique for a particular day. I am storing all these times into an XML file:
Code:
<?xml version="1.0" encoding="utf-8"?>
<_03> <<< March month
<_26> <<< 26th March
<Morning>04:04</Morning>
<Noon>12:05</Noon>
<Afternoon>15:22</Afternoon>
<Sunset>18:26</Sunset>
<Night>19:57</Night>
</_26>
<_27> <<< 27th March
<Morning>04:01</Morning>
<Noon>12:05</Noon>
<Afternoon>15:24</Afternoon>
<Sunset>18:29</Sunset>
<Night>20:00</Night>
</_27>
</_03>
As you see, I sort them like this:
Month
Days in a month
The times for the day
For example:
March
26
Morning: 04:04 Noon: 12:05 and so on
27
Morning 04:01 Noon 12:07 and so on
April
12
Morning 03:42 Noon: 12:33 and so on
13
Morning 03:40 Noon: 12:35 and so on
Do you get the idea? The program first checks the current month and day we are in:
Code:
Dim CurrentDate As Date = Date.Now()
Dim CurrentMonth As String = CurrentDate.ToString("MM")
Dim CurrentDay As String = CurrentDate.ToString("dd")
Then it starts to load the XML. First of all it checks the current month. Today, the CurrentMonth will give you "03" since we are in March. It then opens the XML, and looks for the "_03" element and loads it up. Then it's time to check what day in the month we are in. CurrentDay will give "28" today, since it's 28th March today. So next it loads up the "28" element. In that element there is 5 times for that specific day, the alarm will go on.
Code:
<Morning>04:04</Morning>
<Noon>12:05</Noon>
<Afternoon>15:22</Afternoon>
<Sunset>18:26</Sunset>
<Night>19:57</Night>
So it loads all these 5 times into their own strings:
Code:
Dim Morning As String
Dim Noon As String
Dim Afternoon As String
Dim Sunset As String
Dim Night As String
But I have problems loading the times up correctly from the XML file. Here is my code for loading the times:
Code:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If (IO.File.Exists("data.xml")) Then
Dim document As XmlReader = New XmlTextReader("data.xml")
While (document.Read())
Dim type = document.NodeType
If (type = XmlNodeType.Element) Then
If (document.Name = "_03") Then
If (document.Name = "_" + CurrentDay) Then
TextBox1.Text = document.ReadInnerXml.ToString() ' It's not reaching here for some reason
End If
End If
If (document.Name = "LastName") Then
TextBox2.Text = document.ReadInnerXml.ToString()
End If
End If
End While
End If
End Sub
The XML is like this:
Code:
<?xml version="1.0" encoding="utf-8"?>
<_03>
<_28>
<Morning>04:04</Morning>
<Noon>12:05</Noon>
<Afternoon>15:22</Afternoon>
<Sunset>18:26</Sunset>
<Night>19:57</Night>
</_28>
<_27>
<Morning>04:01</Morning>
<Noon>12:05</Noon>
<Afternoon>15:24</Afternoon>
<Sunset>18:29</Sunset>
<Night>20:00</Night>
</_27>
</_03>
What's the problem?
-
Mar 28th, 2013, 12:42 PM
#2
Re: Reading from XML
If (document.Name = "_03") Then
If (document.Name = "_" + CurrentDay) Then
These two conditions can't both be true simultaneously (well, other than on the 3rd of the month).
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Mar 28th, 2013, 01:27 PM
#3
Thread Starter
Lively Member
Re: Reading from XML
Okay wait I think I understand what you mean..
What is document.Name ? Do you mean only the _03 is the document? Then what is the other stuff inside the _03 ? Are they called elements? And if so how do I load them?
-
Mar 28th, 2013, 02:35 PM
#4
Re: Reading from XML
try this.
this is the xml i used:
Code:
<?xml version="1.0" encoding="utf-8"?>
<months>
<_03>
<_28>
<Morning>04:04</Morning>
<Noon>12:05</Noon>
<Afternoon>15:22</Afternoon>
<Sunset>18:26</Sunset>
<Night>19:57</Night>
</_28>
<_27>
<Morning>04:01</Morning>
<Noon>12:05</Noon>
<Afternoon>15:24</Afternoon>
<Sunset>18:29</Sunset>
<Night>20:00</Night>
</_27>
</_03>
</months>
i read it into an xDocument + queried that with LINQ + displayed the output in labels:
Code:
Public Class Form1
Dim xml As XDocument
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
xml = XDocument.Load("C:\Users\Paul\Desktop\xml.txt")
ComboBox2.DataSource = Enumerable.Range(1, 12).Select(Function(x) MonthName(x)).ToArray
ComboBox1.DataSource = New String() {"_27", "_28"}
End Sub
Private Sub ComboBoxes_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged, ComboBox2.SelectedIndexChanged
If ComboBox1.SelectedIndex = -1 Or ComboBox2.SelectedIndex = -1 Then Return
Dim monthString As String = "_" & (ComboBox2.SelectedIndex + 1).ToString("00")
Dim month As XElement = xml.Root.Element(monthString)
If month IsNot Nothing Then
Label1.Text = month.Elements(ComboBox1.Text)(0).Element("Morning").Value
Label2.Text = month.Elements(ComboBox1.Text)(0).Element("Noon").Value
Label3.Text = month.Elements(ComboBox1.Text)(0).Element("Afternoon").Value
Label4.Text = month.Elements(ComboBox1.Text)(0).Element("Sunset").Value
Label5.Text = month.Elements(ComboBox1.Text)(0).Element("Night").Value
End If
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 28th, 2013, 04:37 PM
#5
Thread Starter
Lively Member
Re: Reading from XML
27 and 28 was just examples. I will put 30 days into each month. Will I have to write them all here too?
Code:
ComboBox1.DataSource = New String() {"_27", "_28"}
Also, what is the combobox for?
-
Mar 28th, 2013, 05:58 PM
#6
Re: Reading from XML
 Originally Posted by Crystalii
27 and 28 was just examples. I will put 30 days into each month. Will I have to write them all here too?
Code:
ComboBox1.DataSource = New String() {"_27", "_28"}
Also, what is the combobox for?
why 30? max number of days in any month is 31...
i used comboboxes as you need some way of choosing a month + a day to read from the xml
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 28th, 2013, 06:51 PM
#7
Thread Starter
Lively Member
Re: Reading from XML
Yeah 31 then.. The amount of days in every month must be exact, so some months got 31 days and some 30. So it really depends for each month if it is 30 or 31. The user will not be able to choose the month and day to read from the XML. The program will check the current month and day, and then it will load up the times for the current day.
Code:
Dim CurrentDate As Date = Date.Now()
Dim CurrentMonth As String = CurrentDate.ToString("MM")
Dim CurrentDay As String = CurrentDate.ToString("dd")
So in Form1_Load, I will load the times from the XML for the current day, then I will save them in the memory as:
Code:
Dim Morning As String
Dim Noon As String
Dim Afternoon As String
Dim Sunset As String
Dim Night As String
All i'll do is load the times from the XML for the current day into these strings. No need to display them in a label. Also is there any other way to read XML files? I am not a big fan of LINQ :/
Oh btw, the reason I have a _ before the date is because I can't have digits in element names. I have to put the _ there or it will give me an error.
Last edited by Crystalii; Mar 28th, 2013 at 08:02 PM.
-
Mar 28th, 2013, 08:00 PM
#8
Re: Reading from XML
ok. using your underscore values, here's an example of how to show months in combobox1 + the correct number of days in combobox2:
Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ComboBox1.DataSource = Enumerable.Range(1, 12).Select(Function(x) MonthName(x)).ToArray
End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim year As Integer = Now.Year
ComboBox2.DataSource = Enumerable.Range(1, Date.DaysInMonth(year, CInt(ComboBox1.SelectedIndex) + 1)).Select(Function(x) "_" & x.ToString).ToArray
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 28th, 2013, 08:15 PM
#9
Thread Starter
Lively Member
Re: Reading from XML
Wait your misunderstanding me.. I don't want to put them into a ComboBox. All I want to do is load the times into my program as I said earlier:
For now I put 2 example days, 27 and 28 March. So let's use them for now, just to get the code working. So basically I just want to load the times from the _28 column into the
Code:
Dim Morning As String
Dim Noon As String
Dim Afternoon As String
Dim Sunset As String
Dim Night As String
No need for combobox or anything else. I just want to load the times from the <_28> here:
Code:
<?xml version="1.0" encoding="utf-8"?>
<_03>
<_28>
<Morning>04:04</Morning>
<Noon>12:05</Noon>
<Afternoon>15:22</Afternoon>
<Sunset>18:26</Sunset>
<Night>19:57</Night>
</_28>
<_27>
<Morning>04:01</Morning>
<Noon>12:05</Noon>
<Afternoon>15:24</Afternoon>
<Sunset>18:29</Sunset>
<Night>20:00</Night>
</_27>
</_03>
to the
Code:
Dim Morning As String
Dim Noon As String
Dim Afternoon As String
Dim Sunset As String
Dim Night As String
So all I really want to do is load this:
Code:
<_28>
<Morning>04:04</Morning>
<Noon>12:05</Noon>
<Afternoon>15:22</Afternoon>
<Sunset>18:26</Sunset>
<Night>19:57</Night>
</_28>
-
Mar 28th, 2013, 08:28 PM
#10
Re: Reading from XML
ok. you want to read from an xml file that has months with day nodes
how do you expect to choose a month + day???
i was trying to answer your unclear question in a way that would work
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 28th, 2013, 09:06 PM
#11
Thread Starter
Lively Member
Re: Reading from XML
I'm not supposed to choose a month + day. It should automaticly choose the month + day. Look, I will have an XML file with 12 months: January, February, March, April and so on........ Each month will be labeled by it's number, like 01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12
Then in each month, there will be 30 or 31 days depending on the specific month. For example if the month is March, then there will be 31 days, since March has 31 days. If it's April then it's 30 days, because April has 30 days only.
Then in each day, there will be 5 times.
Now to the loading part. I will not choose what day I want to load. The program will look at date today. For example if I open the program in 29 March, then the program will look at the date.. it is March 29.. Okay.. So it goes to the XML, searches for 03, then in 03 it loads 29, and in 29 there is 5 times. It loads the 5 times.
See? So it just loads the times for the day you are in, automaticly..
-
Mar 29th, 2013, 09:08 AM
#12
Re: Reading from XML
the method is exactly the same however you select month + day:
Code:
Dim monthString As String = "_" & now.month.ToString("00")
Dim month As XElement = xml.Root.Element(monthString)
If month IsNot Nothing Then
Label1.Text = month.Elements("_" & now.day.ToString("00"))(0).Element("Morning").Value
Label2.Text = month.Elements("_" & now.day.ToString("00"))(0).Element("Noon").Value
Label3.Text = month.Elements("_" & now.day.ToString("00"))(0).Element("Afternoon").Value
Label4.Text = month.Elements("_" & now.day.ToString("00"))(0).Element("Sunset").Value
Label5.Text = month.Elements(("_" & now.day.ToString("00"))(0).Element("Night").Value
End If
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 29th, 2013, 10:58 AM
#13
Thread Starter
Lively Member
Re: Reading from XML
It's not working :/
This is the code I used:
* *
Code:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim xml As XDocument
xml = XDocument.Load(Application.StartupPath & "\data.xml")
Dim monthString As String = "_" & Now.Month.ToString("00")
Dim month As XElement = xml.Root.Element(monthString)
If month IsNot Nothing Then ' <<<< problem is here
Morning = month.Elements("_" & Now.Day.ToString("00"))(0).Element("Morning").Value
'Label2.Text = month.Elements("_" & Now.Day.ToString("00"))(0).Element("Noon").Value
'Label3.Text = month.Elements("_" & Now.Day.ToString("00"))(0).Element("Afternoon").Value
'Label4.Text = month.Elements("_" & Now.Day.ToString("00"))(0).Element("Sunset").Value
'Label5.Text = month.Elements(("_" & now.day.ToString("00"))(0).Element("Night").Value
MsgBox("Hello")
End If
End Sub
I analyzed the code, the problem is that 'month' is nothing here:
Code:
If month IsNot Nothing Then
so it just jumps over.
-
Mar 29th, 2013, 11:24 AM
#14
Re: Reading from XML
what does monthString equal?
if month is nothing, then that node doesn't exist in your xml
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 29th, 2013, 11:27 AM
#15
Re: Reading from XML
also, what is the actual format of data.xml?
the code i gave you works with the xml i posted in post#4
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 29th, 2013, 11:29 AM
#16
Thread Starter
Lively Member
Re: Reading from XML
monthString equals "_03"
This is my XML:
Code:
<?xml version="1.0" encoding="utf-8"?>
<_03>
<_29>
<Morning>04:04</Morning>
<Noon>12:05</Noon>
<Afternoon>15:22</Afternoon>
<Sunset>18:26</Sunset>
<Night>19:57</Night>
</_29>
<_27>
<Morning>04:01</Morning>
<Noon>12:05</Noon>
<Afternoon>15:24</Afternoon>
<Sunset>18:29</Sunset>
<Night>20:00</Night>
</_27>
</_03>
I replaced the _28 with _29 since it is 29 March today.
Edit: It worked! I noticed that you had modified your XML, so I did the same, I put <months> before the <_03>, like this:
Code:
<?xml version="1.0" encoding="utf-8"?>
<months>
<_03>
<_29>
<Morning>04:04</Morning>
<Noon>12:05</Noon>
<Afternoon>15:22</Afternoon>
<Sunset>18:26</Sunset>
<Night>19:57</Night>
</_29>
<_27>
<Morning>04:01</Morning>
<Noon>12:05</Noon>
<Afternoon>15:24</Afternoon>
<Sunset>18:29</Sunset>
<Night>20:00</Night>
</_27>
</_03>
</months>
Thank you very much!
Edit: Does it matter if I change the way you calculated the current day and current month?
You calculated the current day and month like this:
Code:
Now.Day.ToString("00")
Now.Month.ToString("00")
I calculate it like this:
Code:
Dim CurrentDate As Date = Date.Now()
Dim CurrentMonth As String = CurrentDate.ToString("MM")
Dim CurrentDay As String = CurrentDate.ToString("dd")
Which way is better?
Last edited by Crystalii; Mar 29th, 2013 at 11:43 AM.
-
Mar 29th, 2013, 11:58 AM
#17
Re: Reading from XML
they both produce the same result.
either way is ok. your choice as it's your app.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 29th, 2013, 01:58 PM
#18
Frenzied Member
Re: Reading from XML
 Originally Posted by Crystalii
Yeah 31 then.. The amount of days in every month must be exact, so some months got 31 days and some 30. So it really depends for each month if it is 30 or 31.
What about 29 and 28?
Sean
Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.
-
Mar 29th, 2013, 03:12 PM
#19
Thread Starter
Lively Member
Re: Reading from XML
And 29 and 28 I guess lol..
Btw, do you know any method or any event that gets triggered whenever the minute changes? Like let's say the clock is 6:30 PM, then ofc the clock ticks to 6:31, and the event gets triggered? Because I need a way to check everytime there is a new minute, if it's time for my Morning / Noon / Afternoon / Sunset / Night times... I was thinking a timer that constantly checks the clock, but that would take too much CPU power..
-
Mar 29th, 2013, 03:22 PM
#20
Re: Reading from XML
personally, it's not the way I would have designed the XML ...
Code:
<alarms>
<alarm month="3" day="29" name="Morning" time="04:00" />
<alarm month="3" day="29" name="Noon" time="12:05" />
<alarm month="3" day="29" name="Afternoon" time="15:22" />
<alarm month="3" day="29" name="Sunset" time="18:26" />
<alarm month="3" day="29" name="Night" time="19:57" />
</alarms>
Now with that you can selectNodes where month = current month and day = current date and time >= current time....
-tg
-
Mar 29th, 2013, 03:36 PM
#21
Re: Reading from XML
Code:
Private Sub TestXMLAlarm()
Dim Alarms = <alarms>
<alarm month="3" day="29" name="Morning" time="04:00"/>
<alarm month="3" day="29" name="Noon" time="12:05"/>
<alarm month="3" day="29" name="Afternoon" time="15:22"/>
<alarm month="3" day="29" name="Sunset" time="18:26"/>
<alarm month="3" day="29" name="Night" time="19:57"/>
</alarms>
Dim todaysAlarms = Alarms...<alarm>.Where(Function(f) f.Attribute("month").Value = Today.Month.ToString AndAlso f.Attribute("day").Value = Today.Day.ToString)
For Each alarm In todaysAlarms
MessageBox.Show(String.Format("Alarm for {0} found set for {1}!", alarm.Attribute("name").Value, alarm.Attribute("time").Value))
Next
End Sub
some how seems so much simpler to me... but then again... that could be just me.
-tg
-
Mar 29th, 2013, 07:30 PM
#22
Thread Starter
Lively Member
Re: Reading from XML
Nah I'll stick with the version Paul told me, at least I understand the code. Yours looks very complicated for me :P But thanks anyways..
Do you know of any method so that I can check if it's time for the alarm or not? Like let's say I set the alarm to 05:00 AM, how will I know when it is 05:00 AM?
-
Mar 29th, 2013, 08:11 PM
#23
Re: Reading from XML
 Originally Posted by Crystalii
I set the alarm to 05:00 AM, how will I know when it is 05:00 AM?
you'll need to use a timer with an appropriate interval + test the time now against each alarm time
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 29th, 2013, 08:59 PM
#24
Thread Starter
Lively Member
Re: Reading from XML
This is what I did:
Code:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim CurrentTime As String = TimeOfDay.Hour.ToString("00") & ":" & TimeOfDay.Minute.ToString("00")
If CurrentTime = Morning OrElse CurrentTime = Noon OrElse CurrentTime = Afternoon OrElse CurrentTime = Sunset OrElse CurrentTime = Night Then
If CurrentTime = Morning Then
If Not MorningCalled = True Then
MorningCalled = True
MsgBox("Prayer time!")
End If
ElseIf CurrentTime = Noon Then
If Not NoonCalled = True Then
NoonCalled = True
MsgBox("Prayer time!")
End If
ElseIf CurrentTime = Afternoon Then
If Not AfternoonCalled = True Then
AfternoonCalled = True
MsgBox("Prayer time!")
End If
ElseIf CurrentTime = Sunset Then
If Not SunsetCalled = True Then
SunsetCalled = True
MsgBox("Prayer time!")
End If
ElseIf CurrentTime = Night Then
If Not NightCalled = True Then
NightCalled = True
MsgBox("Prayer time!")
End If
End If
End If
End Sub
Code:
Dim Morning As String
Dim Noon As String
Dim Afternoon As String
Dim Sunset As String
Dim Night As String
Dim MorningCalled As Boolean
Dim NoonCalled As Boolean
Dim AfternoonCalled As Boolean
Dim SunsetCalled As Boolean
Dim NightCalled As Boolean
The reason I made a boolean for each alarm period is because the timer would spam "Prayer time!" all the time, since it checks if the time now matches the alarm time every second ( timer interval is set to 1 second ).
-
Mar 29th, 2013, 09:21 PM
#25
Re: Reading from XML
you must be very devout
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 1st, 2013, 09:31 PM
#26
Thread Starter
Lively Member
Re: Reading from XML
I ran into a new problem.. I need to know what alarm time is the next, and display it in a label. For example if the next alarm time is Noon, then I need to display "Next alarm: Noon - 12:05". But I don't know what method I can use to know this?
-
Apr 5th, 2013, 04:48 PM
#27
Thread Starter
Lively Member
Re: Reading from XML
Do you guys know if it is possible to compare the current time with 5 times, then see which of the 5 times is closest to the current time?
Let's say it's 15:10 now, and I got 5 strings, each one is a time.
String 1: 03:22
String 2: 10:02
String 3: 15:08
String 4: 18:34
String 5: 22:19
And in this case it would give 2 outputs, one is the closest time that has already been, in this case it would be String 3, 15:08. And the second output would be the closest time that is going to be, which is String 4, 18:34.
I was thinking of converting the times into integers, then just differate them, and compare them like that. But I got problems combining the minutes with the hours. Also I don't think this is a good way of doing it :S
-
Apr 5th, 2013, 04:58 PM
#28
Re: Reading from XML
Well the first problem is that it's not obvious to the computer (though it may be obvious to you!) whether 03.22 is behind or ahead of 22:19! Unless you impose conditions such as the order of entries counts, it's always best to do all time calculations through full date variables to eradicate the possible ambiguities. You can then use the DateDiff() function to calculate the number of minutes between two times.
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Apr 5th, 2013, 05:21 PM
#29
Thread Starter
Lively Member
Re: Reading from XML
Can you give me an example of the DateDiff?
-
Apr 5th, 2013, 05:32 PM
#30
Thread Starter
Lively Member
Re: Reading from XML
Hello!
I read some about DateDiff()
Here is my try:
Code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim CurrentTime As String = TimeOfDay.Hour.ToString("00") & ":" & TimeOfDay.Minute.ToString("00")
Dim SecondTime As DateTime = Convert.ToDateTime("05:02")
MsgBox(DateDiff(DateInterval.Minute, CurrentTime, SecondTime))
End Sub
-
Apr 5th, 2013, 05:36 PM
#31
Re: Reading from XML
Dim NumberMinutes As Long = DateDiff(DateInterval.Minute, Date1, Date2)
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Apr 5th, 2013, 06:07 PM
#32
Thread Starter
Lively Member
Re: Reading from XML
Thank you! I also noticed that my SecondTime and CurrentTime was not in the same format. So I fixed that. Here is how my code is now:
Code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim SecondTime As DateTime = Convert.ToDateTime("05:02")
Dim CurrentTime As DateTime = DateTime.Now
Dim NumberMinutes As Long = DateDiff(DateInterval.Minute, CurrentTime, SecondTime)
MsgBox(NumberMinutes)
End Sub
Edit: So I edited the code further to suit my situation. Here I got 5 alarm times, and I need to differate them, to see which alarm time is the closest time to the current time.
Code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim MorningTime As DateTime = Convert.ToDateTime(Morning)
Dim NoonTime As DateTime = Convert.ToDateTime(Noon)
Dim AfterNoonTime As DateTime = Convert.ToDateTime(Afternoon)
Dim SunSetTime As DateTime = Convert.ToDateTime(Sunset)
Dim NightTime As DateTime = Convert.ToDateTime(Night)
Dim CurrentTime As DateTime = DateTime.Now
'Dim NumberMinutes As Long = DateDiff(DateInterval.Minute, CurrentTime, MorningTime)
End Sub
Last edited by Crystalii; Apr 5th, 2013 at 06:11 PM.
-
Apr 5th, 2013, 06:32 PM
#33
Re: Reading from XML
here's an example, but here at 00:30, it doesn't work for the reasons that dunfiddlin pointed out.
there is no time that is less than now:
Code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Dim MorningTime As DateTime = Convert.ToDateTime("03:22") 'Morning)
Dim NoonTime As DateTime = Convert.ToDateTime("10:02") 'Noon)
Dim AfterNoonTime As DateTime = Convert.ToDateTime("15:08") 'Afternoon)
Dim SunSetTime As DateTime = Convert.ToDateTime("18:34") 'Sunset)
Dim NightTime As DateTime = Convert.ToDateTime("22:19") 'Night)
Dim allTimes As New Dictionary(Of String, DateTime) From {{"MorningTime", MorningTime}, {"NoonTime", NoonTime}, {"AfterNoonTime", AfterNoonTime}, {"SunSetTime", SunSetTime}, {"NightTime", NightTime}}
Dim CurrentTime As DateTime = DateTime.Now
'Dim NumberMinutes As Long = DateDiff(DateInterval.Minute, CurrentTime, MorningTime)
MsgBox(allTimes.Last(Function(kvp) kvp.Value < CurrentTime).Key)
MsgBox(allTimes.First(Function(kvp) kvp.Value > CurrentTime).Key)
End Sub
End Class
to fix it you could add a midnight value:
Code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Dim MorningTime As DateTime = Convert.ToDateTime("03:22") 'Morning)
Dim NoonTime As DateTime = Convert.ToDateTime("10:02") 'Noon)
Dim AfterNoonTime As DateTime = Convert.ToDateTime("15:08") 'Afternoon)
Dim SunSetTime As DateTime = Convert.ToDateTime("18:34") 'Sunset)
Dim NightTime As DateTime = Convert.ToDateTime("22:19") 'Night)
Dim midnight As DateTime = Convert.ToDateTime("00:00")
Dim allTimes As New Dictionary(Of String, DateTime) From {{"MorningTime", MorningTime}, {"NoonTime", NoonTime}, {"AfterNoonTime", AfterNoonTime}, {"SunSetTime", SunSetTime}, {"NightTime", NightTime}, {"NightTime.", midnight}}
Dim CurrentTime As DateTime = DateTime.Now
'Dim NumberMinutes As Long = DateDiff(DateInterval.Minute, CurrentTime, MorningTime)
MsgBox(allTimes.Last(Function(kvp) kvp.Value < CurrentTime).Key)
MsgBox(allTimes.First(Function(kvp) kvp.Value > CurrentTime).Key)
End Sub
End Class
Last edited by .paul.; Apr 5th, 2013 at 06:36 PM.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 5th, 2013, 08:36 PM
#34
Thread Starter
Lively Member
Re: Reading from XML
Okay I have it working, thanks!
But that midnight created another problem. Look, this way when the clock is 03:00 for example, and the closest previous alarm time is let's say 22:30. It doesn't show the Night, 22:30 as the previous. It shows the midnight, 00:00 as previous, since it is closer to 03:00 than 22:30. We need to somehow make it skip the midnight, and count the time before midnight, in case the previous time would be midnight. This is the code I made:
Code:
Private Sub CheckStatus()
Dim MorningTime As DateTime = Convert.ToDateTime("03:22") 'Morning)
Dim NoonTime As DateTime = Convert.ToDateTime("10:02") 'Noon)
Dim AfterNoonTime As DateTime = Convert.ToDateTime("15:08") 'Afternoon)
Dim SunSetTime As DateTime = Convert.ToDateTime("18:34") 'Sunset)
Dim NightTime As DateTime = Convert.ToDateTime("22:19") 'Night)
Dim midnight As DateTime = Convert.ToDateTime("00:00")
Dim allTimes As New Dictionary(Of String, DateTime) From {{"MorningTime", MorningTime}, {"NoonTime", NoonTime}, {"AfterNoonTime", AfterNoonTime}, {"SunSetTime", SunSetTime}, {"NightTime", NightTime}, {"NightTime.", midnight}}
Dim CurrentTime As DateTime = DateTime.Now
'MsgBox(allTimes.Last(Function(kvp) kvp.Value < CurrentTime).Key) ' Prayer before
'MsgBox(allTimes.First(Function(kvp) kvp.Value > CurrentTime).Key) ' Prayer after
If allTimes.Last(Function(kvp) kvp.Value < CurrentTime).Key.ToString = "MorningTime" Then
lblCurrent.Text = "Current prayer: Morning"
ElseIf allTimes.Last(Function(kvp) kvp.Value < CurrentTime).Key.ToString = "NoonTime" Then
lblCurrent.Text = "Current prayer: Noon"
ElseIf allTimes.Last(Function(kvp) kvp.Value < CurrentTime).Key.ToString = "AfterNoonTime" Then
lblCurrent.Text = "Current prayer: AfterNoon"
ElseIf allTimes.Last(Function(kvp) kvp.Value < CurrentTime).Key.ToString = "SunSetTime" Then
lblCurrent.Text = "Current prayer: Sunset"
ElseIf allTimes.Last(Function(kvp) kvp.Value < CurrentTime).Key.ToString = "NightTime" Then
lblCurrent.Text = "Current prayer: Night"
ElseIf allTimes.Last(Function(kvp) kvp.Value < CurrentTime).Key.ToString = "NightTime." Then ' <<< There you go
lblCurrent.Text = "Current prayer: Night"
End If
If allTimes.First(Function(kvp) kvp.Value > CurrentTime).Key.ToString = "MorningTime" Then
lblNext.Text = "Next prayer: Morning"
ElseIf allTimes.First(Function(kvp) kvp.Value > CurrentTime).Key.ToString = "NoonTime" Then
lblNext.Text = "Next prayer: Noon"
ElseIf allTimes.First(Function(kvp) kvp.Value > CurrentTime).Key.ToString = "AfterNoonTime" Then
lblNext.Text = "Next prayer: AfterNoon"
ElseIf allTimes.First(Function(kvp) kvp.Value > CurrentTime).Key.ToString = "SunSetTime" Then
lblNext.Text = "Next prayer: Sunset"
ElseIf allTimes.First(Function(kvp) kvp.Value > CurrentTime).Key.ToString = "NightTime" Then
lblNext.Text = "Next prayer: Night"
End If
End Sub
However it is giving me the wrong answer. The time is 03:35 at the moment, and Morning time is 03:22 as you can see. So the Previous alarm should be Morning right? But it says the Previous alarm is midnight.
-
Apr 5th, 2013, 08:50 PM
#35
Re: Reading from XML
the easiest way is to remove the midnight time + instead of allTimes.Last + allTimes.First, use allTimes.LastOrDefault + allTimes.FirstOrDefault
you just need to change the words. most of the time it'll work. when it doesn't, it'll return Nothing (which is default)
also the dictionary is Of String, DateTime, which means you could simplify the code by changing the keys:
Code:
Dim allTimes As New Dictionary(Of String, DateTime) From {{"Morning", MorningTime}, 'etc
dim previous as string = allTimes.LastOrDefault(Function(kvp) kvp.Value < CurrentTime).Key
if not previous = Nothing then
lblCurrent.Text = "Current prayer: " & previous
else
'you need to check time here
end if
dim [next] as string = allTimes.FirstOrDefault (Function(kvp) kvp.Value > CurrentTime).Key
if not [next] = Nothing then
lblNext.Text = "Next prayer: " & [next]
else
'you need to check time here
end if
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 5th, 2013, 08:57 PM
#36
Thread Starter
Lively Member
Re: Reading from XML
Okay it all worked! What did you mean by 'you need to check time here ?
-
Apr 5th, 2013, 09:52 PM
#37
Re: Reading from XML
 Originally Posted by Crystalii
Okay it all worked! What did you mean by 'you need to check time here ?
for the reasons I pointed out in post#33
at 00:10 there is no previous time + at 22:30 there is no next time + in both cases previous or [next] will return Nothing
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 8th, 2013, 05:20 PM
#38
Thread Starter
Lively Member
Re: Reading from XML
Okay I did this to solve the problem:
Code:
Public Sub CheckStatus()
Dim MorningTime As DateTime = Convert.ToDateTime(Morning)
Dim NoonTime As DateTime = Convert.ToDateTime(Noon)
Dim AfterNoonTime As DateTime = Convert.ToDateTime(Afternoon)
Dim SunSetTime As DateTime = Convert.ToDateTime(Sunset)
Dim NightTime As DateTime = Convert.ToDateTime(Night)
Dim allTimes As New Dictionary(Of String, DateTime) From {{"Morning", MorningTime}, {"Noon", NoonTime}, {"Afternoon", AfterNoonTime}, {"Sunset", SunSetTime}, {"Night", NightTime}}
Dim CurrentTime As DateTime = DateTime.Now
CurrentPrayer = allTimes.LastOrDefault(Function(kvp) kvp.Value < CurrentTime).Key
If Not CurrentPrayer = Nothing Then
lblCurrent.Text = "Current prayer: " & CurrentPrayer
End If
If CurrentPrayer = "" Then
lblCurrent.Text = "Current prayer: Night "
CurrentPrayer = "Night"
End If
If CurrentPrayer = "Morning" Then
lblNext.Text = "Next prayer: Noon " & Noon
ElseIf CurrentPrayer = "Noon" Then
lblNext.Text = "Next prayer: Afternoon " & Afternoon
ElseIf CurrentPrayer = "Afternoon" Then
lblNext.Text = "Next prayer: Sunset " & Sunset
ElseIf CurrentPrayer = "Sunset" Then
lblNext.Text = "Next prayer: Night " & Night
ElseIf CurrentPrayer = "Night" Then
If Date.Now.Hour >= 0 Then
If Not MorningCalled = True Then
lblNext.Text = "Next prayer: Morning " & GetTime("_" & CurrentMonth, "_" & CurrentDay, "Morning")
ElseIf MorningCalled = True Then
Dim Tomorrow As String = Date.Now.AddDays(1).Day.ToString("00")
lblNext.Text = "Next prayer: Morning " & GetTime("_" & CurrentMonth, "_" & Tomorrow, "Morning")
End If
End If
End If
End Sub
I made it so that if CurrentPrayer is nothing, then it will change CurrentPrayer into "Night". It works good. Then I made it that if CurrentPrayer is "Night" then lblNext.text should be "Morning". This works very good too.
Now my problem is that I display the lblNext.text for example like this "Morning 04:30". I first write what time it is (e.g Morning, Night, Noon and so on...) and then the time (e.g 01:00, 13:02 and so on..).
Now usually, to get the Morning time, I would just use the string "Morning", which is the time for the morning prayer of the current day. However if it is night, and the clock is before 00:00 AM, then the 'Morning' time we want is not the 'Morning' time of the current day, but we want the Morning time of the next day. So for that I made a function that will return the specific time of a specific date I want:
Code:
Private Function GetTime(ByVal monthString As String, ByVal dayString As String, ByVal time As String) As String
Dim xml As XDocument
xml = XDocument.Load(Application.StartupPath & "\data.xml")
Dim TempTime As String
Dim month As XElement = xml.Root.Element(monthString)
If month IsNot Nothing Then
TempTime = month.Elements(dayString)(0).Element(time).Value
Return TempTime
Else
Return ""
End If
End Function
However my problem is how to determine if the clock is before 00:00 AM, and in that case get the time of tomorrow Morning, using my function. Or if the clock is past 00:00, then I should use the 'Morning' for the current day.
-
Apr 9th, 2013, 11:01 AM
#39
Thread Starter
Lively Member
-
Apr 9th, 2013, 12:22 PM
#40
Re: Reading from XML
The question doesn't make any sense. Apart from at 00:00 itself a clock is always simultaneously past 00:00 and before 00:00. If the current time is 13:00 then all times from 00:01 to 12:59 are before 13:00 and therefore must apply, if they are alarms, to the next day. I really can't see where the difficulty lies.
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|