Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27329

Reading from XML

$
0
0
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?

Viewing all articles
Browse latest Browse all 27329

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>