XML-Based Applications: |
|
Introduction to Serialization |
XML has become a platform of choice to create data-based applications. For example, if may employees are working on the same application they share, you can create a common form they would use to create their work orders and save them to a common repository. You can make this possible for almost any type of application. We will apply this concept to our Georgetown Cleaning Services, which is an application used by a dry cleaning store. In this application, a form will be presented to the user who, after processing an order, will click the Save button. The order will be saved to a common file. Eventually, all orders would be saved there and a user will be able to review all orders from that common file. |
Practical Learning: Introducing File-Based Databases |
|
Women Suit Dress Regular Skirt Skirt With Hook Men's Suit 2Pc Men's Suit 3Pc Sweaters Silk Shirt Tie Coat Jacket Swede |
Private Sub dtpTimeLeft_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dtpTimeLeft.ValueChanged Dim dateLeft As DateTime = dtpDateLeft.Value Dim timeLeft As DateTime = dtpTimeLeft.Value Dim time9AM As DateTime = New DateTime(timeLeft.Year, timeLeft.Month, timeLeft.Day, 9, 0, 0) ' If the customer leaves clothes before 9AM... If timeLeft <= time9AM Then ' ... then they should be ready the same day after 5PM dtpDateExpected.Value = dateLeft dtpTimeExpected.Value = New DateTime(dateLeft.Year, dateLeft.Month, dateLeft.Day, 17, 0, 0) Else ' If the clothese were left after 9AM, they will be availablethe following morning at 8AM dtpDateExpected.Value = New DateTime(dateLeft.Year, dateLeft.Month, dateLeft.Day + 1) dtpTimeExpected.Value = New DateTime(dateLeft.Year, dateLeft.Month, dateLeft.Day + 1, 8, 0, 0) End If End Sub |
Private Sub CalculateTotal() Dim unitPriceShirts As Double, unitPricePants As Double, unitPriceItem1 As Double, _ unitPriceItem2 As Double, unitPriceItem3 As Double, unitPriceItem4 As Double Dim subTotalShirts As Double, subTotalPants As Double, subTotalItem1 As Double, _ subTotalItem2 As Double, subTotalItem3 As Double, subTotalItem4 As Double dim qtyShirts as integer = 1, qtyPants as Integer = 1, qtyItem1 as Integer = 1, _ qtyItem2 = 1, qtyItem3 = 1, qtyItem4 = 4 Dim cleaningTotal As Double, taxRate As Double, taxAmount As Double, netPrice As Double ' Retrieve the unit price of this item ' Just in case the user types an invalid value, we are using a try...catch Try unitPriceShirts = CDbl(txtShirtsUnitPrice.Text) Catch fex As FormatException MsgBox("The value you entered for the price of shirts is not valid" & _ vbCrLf & "Please try again") End Try ' Retrieve the number of this item ' Just in case the user types an invalid value, we are using a try...catch Try qtyShirts = CInt(txtShirtsQuantity.Text) Catch fex As FormatException MsgBox("The value you entered for the number of shirts is not valid" & _ vbCrLf & "Please try again") End Try Try unitPricePants = CDbl(txtPantsUnitPrice.Text) Catch fex As FormatException MsgBox("The value you entered for the price of pants is not valid" & _ vbCrLf & "Please try again") End Try Try qtyPants = CInt(txtPantsQuantity.Text) Catch fex As FormatException msgbox("The value you entered for the number of pants is not valid" & _ vbCrLf & "Please try again") End Try Try unitPriceItem1 = CDbl(txtItem1UnitPrice.Text) Catch fex As FormatException msgbox("The value you entered for the price is not valid" & _ vbCrLf & "Please try again") End Try Try qtyItem1 = CInt(txtItem1Quantity.Text) Catch fex As FormatException MsgBox("The value you entered is not valid" & _ vbCrLf & "Please try again") End Try Try unitPriceItem2 = CDbl(txtItem2UnitPrice.Text) Catch fex As FormatException msgbox("The value you entered for the price is not valid" & _ vbCrLf & "Please try again") End Try Try qtyItem2 = CInt(txtItem2Quantity.Text) Catch fex As FormatException msgbox("The value you entered is not valid" & _ vbCrLf & "Please try again") End Try Try unitPriceItem3 = CDbl(txtItem3UnitPrice.Text) Catch fex As FormatException msgbox("The value you entered for the price is not valid" & _ vbCrLf & "Please try again") End Try Try qtyItem3 = CInt(txtItem3Quantity.Text) Catch fex As FormatException msgbox("The value you entered is not valid" & _ vbCrLf & "Please try again") End Try Try unitPriceItem4 = CDbl(txtItem4UnitPrice.Text) Catch fex As FormatException MsgBox("The value you entered for the price is not valid" & _ vbCrLf & "Please try again") End Try Try qtyItem4 = CInt(txtItem4Quantity.Text) Catch fex As FormatException msgbox("The value you entered is not valid" & _ vbCrLf & "Please try again") End Try ' Calculate the sub-total for this item subTotalShirts = qtyShirts * unitPriceShirts subTotalPants = qtyPants * unitPricePants subTotalItem1 = qtyItem1 * unitPriceItem1 subTotalItem2 = qtyItem2 * unitPriceItem2 subTotalItem3 = qtyItem3 * unitPriceItem3 subTotalItem4 = qtyItem4 * unitPriceItem4 ' Calculate the total based on sub-totals cleaningTotal = subTotalShirts + subTotalPants + subTotalItem1 + _ subTotalItem2 + subTotalItem3 + subTotalItem4 taxRate = CDbl(txtTaxRate.Text) ' Calculate the amount owed for the taxes taxAmount = cleaningTotal * taxRate / 100 ' Add the tax amount to the total order netPrice = cleaningTotal + taxAmount ' Display the sub-total in the corresponding text box txtShirtsSubTotal.Text = subTotalShirts.ToString("F") txtPantsSubTotal.Text = subTotalPants.ToString("F") txtItem1SubTotal.Text = subTotalItem1.ToString("F") txtItem2SubTotal.Text = subTotalItem2.ToString("F") txtItem3SubTotal.Text = subTotalItem3.ToString("F") txtItem4SubTotal.Text = subTotalItem4.ToString("F") txtCleaningTotal.Text = cleaningTotal.ToString("F") txtTaxAmount.Text = taxAmount.ToString("F") txtNetPrice.Text = netPrice.ToString("F") End Sub |
Private Sub txtShirtsQuantity_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtShirtsQuantity.Leave CalculateTotal() End Sub |
Private Sub txtPantsQuantity_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtPantsQuantity.Leave CalculateTotal() End Sub |
Private Sub txtItem1Quantity_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtItem1Quantity.Leave If cboItem1.Text <> "None" Then CalculateTotal() Else MsgBox("Make sure you select an item in the combo box") End If End Sub |
Private Sub txtItem2Quantity_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtItem2Quantity.Leave If cboItem2.Text <> "None" Then CalculateTotal() Else MsgBox("Make sure you select an item in the combo box") End If End Sub |
Private Sub txtItem3Quantity_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtItem3Quantity.Leave If cboItem3.Text <> "None" Then CalculateTotal() Else MsgBox("Make sure you select an item in the combo box") End If End Sub |
Private Sub txtItem4Quantity_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtItem4Quantity.Leave If cboItem4.Text <> "None" Then CalculateTotal() Else MsgBox("Make sure you select an item in the combo box") End If End Sub |
Private Sub txtTaxRate_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtTaxRate.Leave CalculateTotal() End Sub |
Private Sub btnClose_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClose.Click End End Sub |
Private Sub btnReset_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReset.Click ' Reset the controls Me.txtCustomerName.Text = "" Me.txtCustomerPhone.Text = "" Me.txtShirtsUnitPrice.Text = "1.25" Me.txtShirtsQuantity.Text = "0" Me.txtShirtsSubTotal.Text = "0.00" Me.txtPantsUnitPrice.Text = "1.95" Me.txtPantsQuantity.Text = "0" Me.txtPantsSubTotal.Text = "0.00" Me.cboItem1.Text = "None" Me.txtItem1UnitPrice.Text = "0.00" Me.txtItem1Quantity.Text = "0" Me.txtItem1SubTotal.Text = "0.00" Me.cboItem2.Text = "None" Me.txtItem2UnitPrice.Text = "0.00" Me.txtItem2Quantity.Text = "0" Me.txtItem2SubTotal.Text = "0.00" Me.cboItem3.Text = "None" Me.txtItem3UnitPrice.Text = "0.00" Me.txtItem3Quantity.Text = "0" Me.txtItem3SubTotal.Text = "0.00" Me.cboItem4.Text = "None" Me.txtItem4UnitPrice.Text = "0.00" Me.txtItem4Quantity.Text = "0" Me.txtItem4SubTotal.Text = "0.00" Me.txtCleaningTotal.Text = "0.00" Me.txtTaxRate.Text = "5.75" Me.txtTaxAmount.Text = "0.00" Me.txtNetPrice.Text = "0.00" Me.txtCustomerName.Focus() End Sub |
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click ' This is the name of the file that holds or will hold the cleaning orders Dim strFilename As String = "CleaningOrders.xml" Dim xmlDoc As XmlDocument = New XmlDocument ' Find out if the file exists already ' If it doesn't, then create it If Not File.Exists(strFilename) Then xmlDoc.LoadXml("<?xml version=""1.0"" encoding=""utf-8""?>" & _ "<CleaningOrders></CleaningOrders>") xmlDoc.Save(strFilename) End If ' Open the XML file xmlDoc.Load(strFilename) ' Locate the CleaningOrder element that holds an individual entry Dim lstOrders As XmlNodeList = xmlDoc.GetElementsByTagName("CleaningOrder") ' If a cleaning order exists already, then increment its number ' Warning: this code assumes that the cleaning orders are not deleted If lstOrders.Count > 0 Then count = lstOrders.Count + 1 ' Create a cleaning order element Dim elmXML As XmlElement = xmlDoc.CreateElement("CleaningOrder") ' Add a receipt number as an attribute of the order elmXML.SetAttribute("ReceiptNumber", CStr(count)) ' Create the child elements of a cleaning order Dim strNewCleaning As String = "<CustomerName>" & Me.txtCustomerName.Text & _ "</CustomerName>" & _ "<CustomerPhone>" & Me.txtCustomerPhone.Text & "</CustomerPhone>" & _ "<DateLeft>" & _ dtpDateLeft.Value.ToString("D", DateTimeFormatInfo.InvariantInfo) & _ "</DateLeft>" & _ "<TimeLeft>" & _ dtpTimeLeft.Value.ToString("t", DateTimeFormatInfo.InvariantInfo) & _ "</TimeLeft>" & _ "<DateExpected>" & _ dtpDateExpected.Value.ToString("D", DateTimeFormatInfo.InvariantInfo) & _ "</DateExpected>" & _ "<TimeExpected>" & _ dtpTimeExpected.Value.ToString("t", DateTimeFormatInfo.InvariantInfo) & _ "</TimeExpected>" & _ "<ShirtsUnitPrice>" & txtShirtsUnitPrice.Text & "</ShirtsUnitPrice>" & _ "<ShirtsQuantity>" & txtShirtsUnitPrice.Text & "</ShirtsQuantity>" & _ "<ShirtsSubTotal>" & txtShirtsSubTotal.Text & "</ShirtsSubTotal>" & _ "<PantsUnitPrice>" & txtShirtsUnitPrice.Text & "</PantsUnitPrice>" & _ "<PantsQuantity>" & txtPantsQuantity.Text & "</PantsQuantity>" & _ "<PantsSubTotal>" & txtPantsSubTotal.Text & "</PantsSubTotal>" & _ "<Item1>" & cboItem1.Text & "</Item1>" & _ "<Item1UnitPrice>" & txtItem1UnitPrice.Text + "</Item1UnitPrice>" & _ "<Item1Quantity>" & txtItem1Quantity.Text + "</Item1Quantity>" & _ "<Item1SubTotal>" & txtItem1SubTotal.Text + "</Item1SubTotal>" & _ "<Item2>" & cboItem2.Text & "</Item2>" & _ "<Item2UnitPrice>" & txtItem2UnitPrice.Text & "</Item2UnitPrice>" & _ "<Item2Quantity>" & txtItem2Quantity.Text & "</Item2Quantity>" & _ "<Item2SubTotal>" & txtItem2SubTotal.Text & "</Item2SubTotal>" & _ "<Item3>" & cboItem3.Text & "</Item3>" & _ "<Item3UnitPrice>" & txtItem3UnitPrice.Text & "</Item3UnitPrice>" & _ "<Item3Quantity>" & txtItem3Quantity.Text & "</Item3Quantity>" & _ "<Item3SubTotal>" & txtItem3SubTotal.Text & "</Item3SubTotal>" & _ "<Item4>" & cboItem4.Text & "</Item4>" & _ "<Item4UnitPrice>" & txtItem4UnitPrice.Text + "</Item4UnitPrice>" & _ "<Item4Quantity>" & txtItem4Quantity.Text & "</Item4Quantity>" & _ "<Item4SubTotal>" & txtItem4SubTotal.Text & "</Item4SubTotal>" & _ "<CleaningTotal>" & txtCleaningTotal.Text & "</CleaningTotal>" & _ "<TaxRate>" & txtTaxRate.Text & "</TaxRate>" & _ "<TaxAmount>" & txtTaxAmount.Text & "</TaxAmount>" & _ "<NetPrice>" & txtNetPrice.Text & "</NetPrice>" elmXML.InnerXml = strNewCleaning ' Add the new cleaning order to the file xmlDoc.DocumentElement.AppendChild(elmXML) ' Save the file xmlDoc.Save(strFilename) ' In case the user will add a new cleaning order, ' increment the receipt number count = count + 1 ' Reset the form in case the user wants ' to add a new cleaning order btnReset_Click(sender, e) End Sub |
Private Sub btnOpen_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOpen.Click Dim strFilename As String = "CleaningOrders.xml" Dim xmlDoc As XmlDocument = New XmlDocument ' If the XML file cannot be found, let the user know and give up If Not File.Exists(strFilename) Then MsgBox("No cleaning order exists or the file cannot be found") Exit Sub End If ' In case the file exists, open it xmlDoc.Load(strFilename) ' Get a list of the elements whose values are CleaningOrder Dim lstOrders As XmlNodeList = xmlDoc.GetElementsByTagName("CleaningOrder") ' Check each cleaning order Dim i As Integer For i = 0 To lstOrders.Count - 1 Step 1 ' If you find a cleaning order with the receipt number If lstOrders.ItemOf(i).Attributes.ItemOf("ReceiptNumber").Value = txtReceiptNumber.Text Then ' Once you find that cleaning order, get the value of each node and display ' them in the form txtCustomerName.Text = lstOrders.ItemOf(i).Item("CustomerName").InnerText txtCustomerPhone.Text = lstOrders.ItemOf(i).Item("CustomerPhone").InnerText Me.dtpDateLeft.Value = Convert.ToDateTime(lstOrders.ItemOf(i).Item("DateLeft").InnerText) Me.dtpTimeLeft.Value = Convert.ToDateTime(lstOrders.ItemOf(i).Item("TimeLeft").InnerText) Me.dtpDateExpected.Value = Convert.ToDateTime(lstOrders.ItemOf(i).Item("DateExpected").InnerText) Me.dtpTimeExpected.Value = Convert.ToDateTime(lstOrders.ItemOf(i).Item("TimeExpected").InnerText) Me.txtShirtsUnitPrice.Text = lstOrders.ItemOf(i).Item("ShirtsUnitPrice").InnerText Me.txtShirtsQuantity.Text = lstOrders.ItemOf(i).Item("ShirtsQuantity").InnerText Me.txtShirtsSubTotal.Text = lstOrders.ItemOf(i).Item("ShirtsSubTotal").InnerText Me.txtPantsUnitPrice.Text = lstOrders.ItemOf(i).Item("PantsUnitPrice").InnerText Me.txtPantsQuantity.Text = lstOrders.ItemOf(i).Item("PantsQuantity").InnerText Me.txtPantsSubTotal.Text = lstOrders.ItemOf(i).Item("PantsSubTotal").InnerText Me.cboItem1.Text = lstOrders.ItemOf(i).Item("Item1").InnerText Me.txtItem1UnitPrice.Text = lstOrders.ItemOf(i).Item("Item1UnitPrice").InnerText Me.txtItem1Quantity.Text = lstOrders.ItemOf(i).Item("Item1Quantity").InnerText Me.txtItem1SubTotal.Text = lstOrders.ItemOf(i).Item("Item1SubTotal").InnerText Me.cboItem2.Text = lstOrders.ItemOf(i).Item("Item2").InnerText Me.txtItem2UnitPrice.Text = lstOrders.ItemOf(i).Item("Item2UnitPrice").InnerText Me.txtItem2Quantity.Text = lstOrders.ItemOf(i).Item("Item2Quantity").InnerText Me.txtItem2SubTotal.Text = lstOrders.ItemOf(i).Item("Item2SubTotal").InnerText Me.cboItem3.Text = lstOrders.ItemOf(i).Item("Item3").InnerText Me.txtItem3UnitPrice.Text = lstOrders.ItemOf(i).Item("Item3UnitPrice").InnerText Me.txtItem3Quantity.Text = lstOrders.ItemOf(i).Item("Item3Quantity").InnerText Me.txtItem3SubTotal.Text = lstOrders.ItemOf(i).Item("Item3SubTotal").InnerText Me.cboItem4.Text = lstOrders.ItemOf(i).Item("Item4").InnerText Me.txtItem4UnitPrice.Text = lstOrders.ItemOf(i).Item("Item4UnitPrice").InnerText Me.txtItem4Quantity.Text = lstOrders.ItemOf(i).Item("Item4Quantity").InnerText Me.txtItem4SubTotal.Text = lstOrders.ItemOf(i).Item("Item4SubTotal").InnerText Me.txtCleaningTotal.Text = lstOrders.ItemOf(i).Item("CleaningTotal").InnerText Me.txtTaxRate.Text = lstOrders.ItemOf(i).Item("TaxRate").InnerText Me.txtTaxAmount.Text = lstOrders.ItemOf(i).Item("TaxAmount").InnerText Me.txtNetPrice.Text = lstOrders.ItemOf(i).Item("NetPrice").InnerText End If Next End Sub |
|
||
Home | Copyright © 2004-2012, FunctionX | |
|