Lab 4 XML
Lab 4 XML
Introduction
This application is an example of creating and managing XML elements using the XmlTextWriter and
the XmlTextReader classes of the .NET Framework, which implement the processes of writing to, and
reading from, an XML file. The application is used by a car repair shop that takes orders and processes
them.
• Label
• Button
• Text Box
• Group Box
1. Start Microsoft Visual Studio .NET and create a Windows Forms Application
named CollegeParkAutoRepair1
2. In the Solution Explorer, right-click Form1.vb and click Rename
3. Type CPAR.vb and press Enter
4. From the Menus & Toolbars section of the Toolbox, click MenuStrip and click the form
5. Design the menu items as follows:
MenuItem DropDownItems
Text Name Text Name Shortcut
&File mnuFile &New Repair Order mnuFileNew Ctrl+N
&Open Existing
mnuFileOpen Ctrl+O
Order...
&Save Current Order mnuFileSave Ctrl+S
Separator
&Print... mnuFilePrint Ctrl+P
Print Pre&view... mnuFilePrintPreview
Separator
E&xit mnuFileExit
Imports System.IO
Imports System.Xml
Imports System.Text
End Class
9. Under the Public Class CPAR line, create a sub-procedure named Calculate as follows:
Imports System.IO
Imports System.Xml
Try
QuantityPart1 = CInt(txtQuantity1.Text)
Catch Exc As FormatException
MsgBox("Invalid Quantity")
txtQuantity1.Text = "0"
txtQuantity1.Focus()
End Try
End If
Try
QuantityPart2 = CInt(txtQuantity2.Text)
Catch Exc As FormatException
MsgBox("Invalid Quantity")
txtQuantity2.Text = "0"
txtQuantity2.Focus()
End Try
End If
Try
QuantityPart3 = CInt(txtQuantity3.Text)
Catch Exc As FormatException
MsgBox("Invalid Quantity")
txtQuantity3.Text = "0"
txtQuantity3.Focus()
End Try
End If
Try
QuantityPart4 = CInt(txtQuantity4.Text)
Catch Exc As FormatException
MsgBox("Invalid Quantity")
txtQuantity4.Text = "0"
txtQuantity4.Focus()
End Try
End If
Try
QuantityPart5 = CInt(txtQuantity5.Text)
Catch Exc As FormatException
MsgBox("Invalid Quantity")
txtQuantity5.Text = "0"
txtQuantity5.Focus()
End Try
End If
' Don't bill the customer for a job that is not specified
If txtJobDescription1.Text = "" Then
txtJobPrice1.Text = "0.00"
Job1Price = 0.0
Else
Try
job1Price = CDbl(txtJobPrice1.Text)
Catch Exc As FormatException
MsgBox("Invalid Job Price")
txtJobPrice1.Text = "0.00"
txtJobPrice1.Focus()
End Try
End If
Try
TaxRate = CDbl(txtTaxRate.Text)
Catch Exc As FormatException
MsgBox("Invalid Tax Rate")
txtTaxRate.Text = "7.75"
txtTaxRate.Focus()
End Try
txtTotalParts.Text = FormatNumber(TotalParts)
txtTotalLabor.Text = FormatNumber(TotalLabor)
txtTaxAmount.Text = FormatNumber(TaxAmount)
txtTotalOrder.Text = FormatNumber(TotalOrder)
End Sub
End Class
txtPartName1.Text = ""
txtUnitPrice1.Text = "0.00"
txtQuantity1.Text = "0"
txtSubTotal1.Text = "0.00"
txtPartName2.Text = ""
txtUnitPrice2.Text = "0.00"
txtQuantity2.Text = "0"
txtSubTotal2.Text = "0.00"
txtPartName3.Text = ""
txtUnitPrice3.Text = "0.00"
txtQuantity3.Text = "0"
txtSubTotal3.Text = "0.00"
txtPartName4.Text = ""
txtUnitPrice4.Text = "0.00"
txtQuantity4.Text = "0"
txtSubTotal4.Text = "0.00"
txtPartName5.Text = ""
txtUnitPrice5.Text = "0.00"
txtQuantity5.Text = "0"
txtSubTotal5.Text = "0.00"
txtJobDescription1.Text = ""
txtJobPrice1.Text = "0.00"
txtJobDescription2.Text = ""
txtJobPrice2.Text = "0.00"
txtJobDescription3.Text = ""
txtJobPrice3.Text = "0.00"
txtJobDescription4.Text = ""
txtJobPrice4.Text = "0.00"
txtJobDescription5.Text = ""
txtJobPrice5.Text = "0.00"
txtTotalParts.Text = "0.00"
txtTotalLabor.Text = "0.00"
txtTaxRate.Text = "7.75"
txtTaxAmount.Text = "0.00"
txtTotalOrder.Text = "0.00"
txtRecommendations.Text = ""
txtCustomerName.Focus()
End Sub
12. Under the above End Sub line, create the following common eventand click the first text
box under Unit Price
13. In the Properties window, click the Events button and double-click Leave
14. Call the Calculate() method as follows:
txtUnitPrice2.Leave, _
txtUnitPrice3.Leave, _
txtUnitPrice4.Leave, _
txtUnitPrice5.Leave, _
txtQuantity1.Leave, _
txtQuantity2.Leave, _
txtQuantity3.Leave, _
txtQuantity4.Leave, _
txtQuantity5.Leave, _
txtJobPrice1.Leave, _
txtJobPrice2.Leave, _
txtJobPrice3.Leave, _
txtJobPrice4.Leave, _
txtJobPrice5.Leave, _
txtTaxRate.Leave
Calculate()
End Sub
e.Graphics.DrawString(txtPartName1.Text, fntString, _
Brushes.Black, 100, 490)
e.Graphics.DrawString(txtUnitPrice1.Text, fntString, _
Brushes.Black, 480, 490, fmtString)
e.Graphics.DrawString(txtQuantity1.Text, fntString, _
Brushes.Black, 540, 490, fmtString)
e.Graphics.DrawString(txtSubTotal1.Text, fntString, _
Brushes.Black, 630, 490, fmtString)
e.Graphics.DrawLine(New Pen(Color.Black, 1), _
100, 510, 640, 510)
e.Graphics.DrawString(txtPartName2.Text, fntString, _
Brushes.Black, 100, 520)
e.Graphics.DrawString(txtUnitPrice2.Text, fntString, _
Brushes.Black, 480, 520, fmtString)
e.Graphics.DrawString(txtQuantity2.Text, fntString, _
Brushes.Black, 540, 520, fmtString)
e.Graphics.DrawString(txtSubTotal2.Text, fntString, _
Brushes.Black, 630, 520, fmtString)
e.Graphics.DrawLine(New Pen(Color.Black, 1), _
100, 540, 640, 540)
e.Graphics.DrawString(txtPartName3.Text, fntString, _
Brushes.Black, 100, 550)
e.Graphics.DrawString(txtUnitPrice3.Text, fntString, _
Brushes.Black, 480, 550, fmtString)
e.Graphics.DrawString(txtQuantity3.Text, fntString, _
Brushes.Black, 540, 550, fmtString)
e.Graphics.DrawString(txtSubTotal3.Text, fntString, _
Brushes.Black, 630, 550, fmtString)
e.Graphics.DrawLine(New Pen(Color.Black, 1), _
100, 570, 640, 570)
e.Graphics.DrawString(txtPartName4.Text, fntString, _
Brushes.Black, 100, 580)
e.Graphics.DrawString(txtUnitPrice4.Text, fntString, _
Brushes.Black, 480, 580, fmtString)
e.Graphics.DrawString(txtQuantity4.Text, fntString, _
Brushes.Black, 540, 580, fmtString)
e.Graphics.DrawString(txtSubTotal4.Text, fntString, _
Brushes.Black, 630, 580, fmtString)
e.Graphics.DrawLine(New Pen(Color.Black, 1), _
100, 600, 640, 600)
e.Graphics.DrawString(txtPartName5.Text, fntString, _
Brushes.Black, 100, 610)
e.Graphics.DrawString(txtUnitPrice5.Text, fntString, _
Brushes.Black, 480, 610, fmtString)
e.Graphics.DrawString(txtQuantity5.Text, fntString, _
Brushes.Black, 540, 610, fmtString)
e.Graphics.DrawString(txtSubTotal5.Text, fntString, _
Brushes.Black, 630, 610, fmtString)
e.Graphics.DrawLine(New Pen(Color.Black, 1), _
100, 630, 640, 630)
e.Graphics.DrawString(txtJobDescription1.Text, fntString, _
Brushes.Black, 100, 710)
e.Graphics.DrawString(txtJobPrice1.Text, fntString, _
Brushes.Black, 600, 710, fmtString)
e.Graphics.DrawLine(New Pen(Color.Black, 1), _
100, 730, 640, 730)
e.Graphics.DrawString(txtJobDescription2.Text, fntString, _
Brushes.Black, 100, 740)
e.Graphics.DrawString(txtJobPrice2.Text, fntString, _
Brushes.Black, 600, 740, fmtString)
e.Graphics.DrawLine(New Pen(Color.Black, 1), _
100, 760, 640, 760)
e.Graphics.DrawString(txtJobDescription3.Text, fntString, _
Brushes.Black, 100, 770)
e.Graphics.DrawString(txtJobPrice3.Text, fntString, _
Brushes.Black, 600, 770, fmtString)
e.Graphics.DrawLine(New Pen(Color.Black, 1), _
100, 790, 640, 790)
e.Graphics.DrawString(txtJobDescription4.Text, _
fntString, Brushes.Black, 100, 800)
e.Graphics.DrawString(txtJobPrice4.Text, fntString, _
Brushes.Black, 600, 800, fmtString)
e.Graphics.DrawLine(New Pen(Color.Black, 1), _
100, 820, 640, 820)
e.Graphics.DrawString(txtJobDescription5.Text, _
fntString, Brushes.Black, 100, 830)
e.Graphics.DrawString(txtJobPrice5.Text, fntString, _
Brushes.Black, 600, 830, fmtString)
e.Graphics.DrawLine(New Pen(Color.Black, 1), _
100, 850, 640, 850)
' Get the list of files, if any, from the above folder
Dim ListOfFiles() As FileInfo = Folder.GetFiles("*.xml")
' Find out if the user clicked OK after displaying the Save dialog box
If dlgSave.ShowDialog() = DialogResult.OK Then
Dim wtrRepairOrder As XmlTextWriter = New XmlTextWriter(Filename, _
Encoding.UTF8)
wtrRepairOrder.WriteStartElement("RepairOrders")
wtrRepairOrder.WriteStartElement("Invoice")
wtrRepairOrder.WriteElementString("CustomerName", txtCustomerName.Text)
wtrRepairOrder.WriteElementString("Addres", txtAddress.Text)
wtrRepairOrder.WriteElementString("City", txtCity.Text)
wtrRepairOrder.WriteElementString("State", txtState.Text)
wtrRepairOrder.WriteElementString("ZIPCode", txtZIPCode.Text)
wtrRepairOrder.WriteElementString("Make", txtMake.Text)
wtrRepairOrder.WriteElementString("Model", txtModel.Text)
wtrRepairOrder.WriteElementString("CarYear", txtCarYear.Text)
wtrRepairOrder.WriteElementString("ProbDescription", txtProblem.Text)
wtrRepairOrder.WriteElementString("PartName1", txtPartName1.Text)
wtrRepairOrder.WriteElementString("UnitPrice1", txtUnitPrice1.Text)
wtrRepairOrder.WriteElementString("Quantity1", txtQuantity1.Text)
wtrRepairOrder.WriteElementString("SubTotal1", txtSubTotal1.Text)
wtrRepairOrder.WriteElementString("PartName2", txtPartName2.Text)
wtrRepairOrder.WriteElementString("UnitPrice2", txtUnitPrice2.Text)
wtrRepairOrder.WriteElementString("Quantity2", txtQuantity2.Text)
wtrRepairOrder.WriteElementString("SubTotal2", txtSubTotal2.Text)
wtrRepairOrder.WriteElementString("PartName3", txtPartName3.Text)
wtrRepairOrder.WriteElementString("UnitPrice3", txtUnitPrice3.Text)
wtrRepairOrder.WriteElementString("Quantity3", txtQuantity3.Text)
wtrRepairOrder.WriteElementString("SubTotal3", txtSubTotal3.Text)
wtrRepairOrder.WriteElementString("PartName4", txtPartName4.Text)
wtrRepairOrder.WriteElementString("UnitPrice4", txtUnitPrice4.Text)
wtrRepairOrder.WriteElementString("Quantity4", txtQuantity4.Text)
wtrRepairOrder.WriteElementString("SubTotal4", txtSubTotal4.Text)
wtrRepairOrder.WriteElementString("PartName5", txtPartName5.Text)
wtrRepairOrder.WriteElementString("UnitPrice5", txtUnitPrice5.Text)
wtrRepairOrder.WriteElementString("Quantity5", txtQuantity5.Text)
wtrRepairOrder.WriteElementString("SubTotal5", txtSubTotal5.Text)
wtrRepairOrder.WriteElementString("JobDescription1", _
txtJobDescription1.Text)
wtrRepairOrder.WriteElementString("JobPrice1", txtJobPrice1.Text)
wtrRepairOrder.WriteElementString("JobDescription2", _
txtJobDescription2.Text)
wtrRepairOrder.WriteElementString("JobPrice2", txtJobPrice2.Text)
wtrRepairOrder.WriteElementString("JobDescription3", _
txtJobDescription3.Text)
wtrRepairOrder.WriteElementString("JobPrice3", txtJobPrice3.Text)
wtrRepairOrder.WriteElementString("JobDescription4", _
txtJobDescription4.Text)
wtrRepairOrder.WriteElementString("JobPrice4", txtJobPrice4.Text)
wtrRepairOrder.WriteElementString("JobDescription5", _
txtJobDescription5.Text)
wtrRepairOrder.WriteElementString("JobPrice5", txtJobPrice5.Text)
wtrRepairOrder.WriteElementString("TotalPart", txtTotalParts.Text)
wtrRepairOrder.WriteElementString("TotalLabor", txtTotalLabor.Text)
wtrRepairOrder.WriteElementString("TaxRate", txtTaxRate.Text)
wtrRepairOrder.WriteElementString("TaxAmount", txtTaxAmount.Text)
wtrRepairOrder.WriteElementString("TotalOrder", txtTotalOrder.Text)
wtrRepairOrder.WriteElementString("Recommendation", _
txtRecommendations.Text)
wtrRepairOrder.WriteEndElement()
wtrRepairOrder.WriteEndElement()
wtrRepairOrder.WriteEndDocument()
wtrRepairOrder.Flush()
wtrRepairOrder.Close()
End If
End Sub
47. Save it
48. Close the form and return to your programming environment
49. Display the form.
From the Dialogs section of the Toolbox, click OpenFileDialog and click the form
50. Change its properties as follows:
Title: Open Existing Repair Order
DefaultExt: xml
Filter: Repair Orders (*.xml)|*.xml|All Files|
Name: dlgOpen
51. Right-click the form and click View Code
52. In the Class Name combo box, select mnuFileOpen
53. In the Method Name combo box, select Click and implement the event as follows:
Try
If dlgOpen.ShowDialog() = DialogResult.OK Then
rdrRepairOrder = New XmlTextReader(dlgOpen.FileName)
54. Execute the application and open one the previous orders
55. In the Class Name combo box, select mnuFileSave and, to control the indentation when
the file is saved, change the top section of the event as follows:
' Find out if the user clicked OK after displaying the Save
dialog box
If dlgSave.ShowDialog() = DialogResult.OK Then
Dim wtrRepairOrder As XmlTextWriter = New
XmlTextWriter(Filename, _
Encoding.UTF8)
wtrRepairOrder.Formatting = Formatting.Indented
wtrRepairOrder.Indentation = 4
. . . No Change
End If
End Sub