#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