#4 - XML Reader & Writer

Back-End Visual Basic Code

front-end User Interface

Imports System.Xml


Public Class _Default

    Inherits Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

    End Sub

    Protected Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles txtDescript.TextChanged

    End Sub

    Protected Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles txtCode.TextChanged

    End Sub

    Protected Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click





    End Sub

    Protected Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click

        ' get Textbox data off the Screen Form into our Program Variables
        ' -- not doing any User Input Validation for this data
        'The Output File will be written into the Bin/Debug Folder of this project since no defined file path name

        Dim strCode As String
        Dim strDescript As String
        Dim strICD9 As String

        strCode = txtCode.Text
        strDescript = txtDescript.Text
        strICD9 = txtICD9.Text
        '//diagnosis[@code='a04.7']
        Dim writer As New XmlTextWriter("c:\Testingwriter.xml", System.Text.Encoding.UTF8)
        writer.WriteStartDocument(True)

        '--Format Indentations into the XML output file
        writer.Formatting = Formatting.Indented
        writer.Indentation = 2

        '--Write the top level tag for the File name as <Diagnoses> 
        writer.WriteStartElement("Diagnoses")

        '--Call a Customized XML Write Function (createRec) passing the required variable
        CreateXMLRec(strCode, strDescript, strICD9, writer)

        '--Write the closing tags for those "Diagnoses" File
        writer.WriteEndElement()
        writer.WriteEndDocument()
        writer.Close()

        '-- Let the User know the record has been Saved to the Output File.
        MsgBox("Diagnoses have been updated successfully!")

    End Sub
    '--custom function CreateXMLRec that writes the XML document
    Private Function CreateXMLRec(ByVal IncomingCode As String, ByVal IncomingDescript As String,
                                          ByVal IncomingICD9 As String, ByVal writer As XmlTextWriter)
        '--write the Start Xml Tag for the "Diagnosis" Record
        writer.WriteStartElement("Diagnosis")

        writer.WriteStartElement("Code")
        writer.WriteString(IncomingCode)
        writer.WriteEndElement()

        writer.WriteStartElement("Description")
        writer.WriteString(IncomingDescript)
        writer.WriteEndElement()

        writer.WriteStartElement("ICD9")
        writer.WriteString(IncomingICD9)
        writer.WriteEndElement()


        '--write the End XML Tag for the "Diagnosis" Record
        writer.WriteEndElement()

        '--note this Function just writes but does not return any values
    End Function

    Protected Sub btnShow_Click(sender As Object, e As EventArgs) Handles btnShow.Click
        '-- function used to read XML Files
        Dim strCode As String
        Dim strDescript As String
        Dim strICD9 As String
        Dim strMessage As String

        '--Definate variables required for reading the XML File
        Dim m_xmld As XmlDocument
        Dim m_nodelist As XmlNodeList
        Dim m_node As XmlNode

        m_xmld = New XmlDocument
        '"Testingwriter.xml" is the input file that i copied into the /bin
        m_xmld.Load("c:/Testingwriter.xml")     'this loads up the file we want to read
        '"diagnosis" is the node identifier tag in our file to enclose each record
        m_nodelist = m_xmld.GetElementsByTagName("Diagnosis")   'tells XML when you spot a "Diagnosis" that separates each record

        '--use a for..next loop to read through all of the records in the XMl File
        '-- and Display their contents one by one using a simple Message Box Display
        For Each m_node In m_nodelist    'each time we find a Diagnosis, do this stuff then go to the next one
            strCode = m_node.Item("Code").InnerText   '.innertext  says "go inside the <></> and grab the text in the middle of the tags
            strDescript = m_node.Item("Description").InnerText
            strICD9 = m_node.Item("ICD9").InnerText

            strMessage = "Code:              " & strCode & vbCrLf _
            & "Description:                         " & strDescript & vbCrLf _
            & "ICD9:                       " & strICD9

            MsgBox(strMessage)
        Next
    End Sub

    'Protected Sub btnShow_Click(sender As Object, e As EventArgs) Handles btnShow.Click  'VERSION 2
    '    '-- function used to read XML Files VERSION 2
    '    Dim strCode As String
    '    Dim strDescript As String
    '    Dim strICD9 As String
    '    Dim strMessage As String

    '    '--Definate variables required for reading the XML File
    '    Dim m_xmld As XmlDocument
    '    Dim m_nodelist As XmlNodeList
    '    Dim m_node As XmlNode

    '    m_xmld = New XmlDocument
    '    '"Testingwriter.xml" is the input file that i copied into the /bin
    '    m_xmld.Load("c:/Testingwriter.xml")     'this loads up the file we want to read
    '    '"diagnosis" is the node identifier tag in our file to enclose each record
    '    m_nodelist = m_xmld.GetElementsByTagName("Diagnosis")   'tells XML when you spot a "Diagnosis" that separates each record

    '    '--use a for..next loop to read through all of the records in the XMl File
    '    '-- and Display their contents one by one using a simple Message Box Display
    '    For Each m_node In m_nodelist    'each time we find a Diagnosis, do this stuff then go to the next one
    '        strCode = m_node.Item("Code").InnerText   '.innertext  says "go inside the <></> and grab the text in the middle of the tags
    '        strDescript = m_node.Item("Description").InnerText
    '        strICD9 = m_node.Item("ICD9s").InnerText


    '        '--Add each field value into its List Box on the Form as we loop through the records
    '        lstCode.Items.Add(strCode)
    '        lstDescript.Items.Add(strDescript)
    '        lstICD9.Items.Add(strICD9)


    '        GroupBox(strMessage)
    '    Next
    'End Sub
End Class

#3 - If-Then Logic

Module Module1

    Sub Main()  'adding conditional logic to our operations

        'Console.WriteLine("Bob's Big Giveaway!") 'display a string in console
        'Console.Write("Would you prefer what is behind door number 1,2 or 3?") 'gets user input
        'Dim userValue As String = Console.ReadLine()  'stores console data as string variable userValue

        'Dim message As String = ""  'declares variable message as String datatype, stores message as string ""
        'If userValue = "1" Then      'checks if stringtype variable userValue is "1", if true then execute line 11, if false execute line 12
        '    message = "You won a new car!"
        'ElseIf userValue = "2" Then 'Else If creates another check in the If-Then statement
        '    message = "You won a new boat!"
        'ElseIf userValue = "3" Then 'Else If creates another check in the If-Then statement
        '    message = "You won a new cat!"
        'Else  'catch-all 
        '    message = "bad input! Please play again"

        'End If
        'Console.WriteLine(message)  'writes then displays the stringtype variable message
        'Console.ReadLine()

        Console.WriteLine("Bob's Big Giveaway!")
        Console.Write("Would you prefer what is behind door number 1,2 or 3?")
        Dim userValue As String = Console.ReadLine()

        Dim message As String = ""
        message = IIf(userValue = "1", "Car", "Cat")
        Console.WriteLine("You won a {0}!", message)  'curly brackets {} act as a replacement string that is replaces with var message
        Console.ReadLine()
    End Sub

End Module

#2 - Data Types and Variables

Module Module1

    Sub Main()  'A variable is a bucket in the computers memory that you can story data in and retrieve data out of it, the data being declared in the code. 

        'X = 7   , this needs to be rewritten so Visual Basic knows 
        'Y = X + 3
        Dim x As Integer 'Dim is short for Dimension, this will declare variables datatypes 
        ' With this we are declaring a variable as x and this variable will be an integer
        Dim y As Integer
        'Integers story numbers, 4.3billion is the max in 32bit (2^32), if you consider above and below 0, that gives us +-2,147,483,648 as a max for an integer in a regular bucket
        x = 7 'set the integer named x as 7
        y = x + 3 'store the result of x + 7 as y
        Console.WriteLine(y)

        'here we will demonstrate string datatypes
        Console.WriteLine("What is your name?")  'displays the string "What is your name?" in the console window
        Console.Write("Type your first name: ")  'prompts the user to enter their name, later stored as variable myFirstName
        Dim myFirstName As String = Console.ReadLine() 'declares the variable myFirstName as a string datatype
        'gets a string from  console and stores as a variable myFirstName

        Console.Write("Type your last name: ") 'gets a string to be stored later as myLastName
        Dim myLastName As String = Console.ReadLine()  'declares the variable myLastName as a string
        'gets a string from console and stores as a variable myLastName

        Console.WriteLine("Hello," & myFirstName & " " & myLastName)  '& concatonates stuff
        Console.ReadLine()

    End Sub
End Module

First Project - "Hello World"

first_project.JPG
' <---this key " ' " means you comment out a line of code to in case you are writing something more human readable or meta-codes, the machine ignores this line if commented
Module Module1 'a Module is like a Class. A module in simple terms houses blocks of code that are related together.

    Sub Main() 'defines a block of code, sub is short for sub-routine, "Sub" could also be replaces with "Function", broadly these are methods. "Main" is the name of the codeblock
        ' "Main()" is always the first code that is executed

        Console.WriteLine("Hello World") '  Console. is a class or a container, .WriteLine and .ReadLine are Methods that were already written by other developers. Class.Method
        ' "Hello World" is a literal string of characters which is a type of information, or a datatype String. 
        Console.ReadLine() 'calling a method inside of the .NET framework class library that expects user input. () closed parenthesis says to wait until more instructions

    End Sub 'says this code block is finished, Sub is a Visual Studio Keyword

End Module 'says this code block is finished



'line 7 could be rewritten in 2 lines as
'     Console _
'       .WriteLine("Hello World")