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 |
[STAThread] static void Main() { Application.Run(new OrderProcessing()); } |
|
Women Suit Dress Regular Skirt Skirt With Hook Men's Suit 2Pc Men's Suit 3Pc Sweaters Silk Shirt Tie Coat Jacket Swede |
private void dtpTimeLeft_ValueChanged(object sender, System.EventArgs e) { DateTime dateLeft = this.dtpDateLeft.Value; DateTime timeLeft = this.dtpTimeLeft.Value; DateTime time9AM = new DateTime(timeLeft.Year, timeLeft.Month, timeLeft.Day, 9, 0, 0); // If the customer leaves clothes before 9AM... if( timeLeft <= time9AM ) { // ... then they should be ready the same day after 5PM this.dtpDateExpected.Value = dateLeft; this.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 this.dtpDateExpected.Value = new DateTime(dateLeft.Year, dateLeft.Month, dateLeft.Day + 1); this.dtpTimeExpected.Value = new DateTime(dateLeft.Year, dateLeft.Month, dateLeft.Day + 1, 8, 0, 0); } } |
internal void CalculateTotal() { decimal unitPriceShirts = 0.00M, unitPricePants = 0.00M, unitPriceItem1 = 0.00M, unitPriceItem2 = 0.00M, unitPriceItem3 = 0.00M, unitPriceItem4 = 0.00M; decimal subTotalShirts = 0.00M, subTotalPants = 0.00M, subTotalItem1 = 0.00M, subTotalItem2 = 0.00M, subTotalItem3 = 0.00M, subTotalItem4 = 0.00M; int qtyShirts = 1, qtyPants = 1, qtyItem1 = 1, qtyItem2 = 1, qtyItem3 = 1, qtyItem4 = 4; decimal cleaningTotal = 0.00M, taxRate = 0.00M, taxAmount = 0.00M, netPrice = 0.00M; // Retrieve the unit price of this item // Just in case the user types an invalid value, we are using a try...catch try { unitPriceShirts = decimal.Parse(this.txtShirtsUnitPrice.Text); } catch(FormatException ) { MessageBox.Show("The value you entered for the price of shirts is not valid" + "\nPlease try again"); } // Retrieve the number of this item // Just in case the user types an invalid value, we are using a try...catch try { qtyShirts = int.Parse(this.txtShirtsQuantity.Text); } catch(FormatException ) { MessageBox.Show("The value you entered for the number of shirts is not valid" + "\nPlease try again"); } try { unitPricePants = decimal.Parse(this.txtPantsUnitPrice.Text); } catch(FormatException ) { MessageBox.Show("The value you entered for the price of pants is not valid" + "\nPlease try again"); } try { qtyPants = int.Parse(this.txtPantsQuantity.Text); } catch(FormatException ) { MessageBox.Show("The value you entered for the number of pants is not valid" + "\nPlease try again"); } try { unitPriceItem1 = decimal.Parse(this.txtItem1UnitPrice.Text); } catch(FormatException ) { MessageBox.Show("The value you entered for the price is not valid" + "\nPlease try again"); } try { qtyItem1 = int.Parse(this.txtItem1Quantity.Text); } catch(FormatException ) { MessageBox.Show("The value you entered is not valid" + "\nPlease try again"); } try { unitPriceItem2 = decimal.Parse(this.txtItem2UnitPrice.Text); } catch(FormatException ) { MessageBox.Show("The value you entered for the price is not valid" + "\nPlease try again"); } try { qtyItem2 = int.Parse(this.txtItem2Quantity.Text); } catch(FormatException ) { MessageBox.Show("The value you entered is not valid" + "\nPlease try again"); } try { unitPriceItem3 = decimal.Parse(this.txtItem3UnitPrice.Text); } catch(FormatException ) { MessageBox.Show("The value you entered for the price is not valid" + "\nPlease try again"); } try { qtyItem3 = int.Parse(this.txtItem3Quantity.Text); } catch(FormatException ) { MessageBox.Show("The value you entered is not valid" + "\nPlease try again"); } try { unitPriceItem4 = decimal.Parse(this.txtItem4UnitPrice.Text); } catch(FormatException ) { MessageBox.Show("The value you entered for the price is not valid" + "\nPlease try again"); } try { qtyItem4 = int.Parse(this.txtItem4Quantity.Text); } catch(FormatException ) { MessageBox.Show("The value you entered is not valid" + "\nPlease try again"); } // 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 = decimal.Parse(this.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 this.txtShirtsSubTotal.Text = subTotalShirts.ToString("F"); this.txtPantsSubTotal.Text = subTotalPants.ToString("F"); this.txtItem1SubTotal.Text = subTotalItem1.ToString("F"); this.txtItem2SubTotal.Text = subTotalItem2.ToString("F"); this.txtItem3SubTotal.Text = subTotalItem3.ToString("F"); this.txtItem4SubTotal.Text = subTotalItem4.ToString("F"); this.txtCleaningTotal.Text = cleaningTotal.ToString("F"); this.txtTaxAmount.Text = taxAmount.ToString("F"); this.txtNetPrice.Text = netPrice.ToString("F"); } |
private void txtShirtsQuantity_Leave(object sender, System.EventArgs e) { CalculateTotal(); } |
private void txtPantsQuantity_Leave(object sender, System.EventArgs e) { CalculateTotal(); } |
private void txtItem1Quantity_Leave(object sender, System.EventArgs e) { if( this.cboItem1.Text != "None" ) CalculateTotal(); else MessageBox.Show("Make sure you select an item in the combo box"); } |
private void txtItem2Quantity_Leave(object sender, System.EventArgs e) { if( this.cboItem2.Text != "None" ) CalculateTotal(); else MessageBox.Show("Make sure you select an item in the combo box"); } |
private void txtItem3Quantity_Leave(object sender, System.EventArgs e) { if( this.cboItem3.Text != "None" ) CalculateTotal(); else MessageBox.Show("Make sure you select an item in the combo box"); } |
private void txtItem4Quantity_Leave(object sender, System.EventArgs e) { if( this.cboItem4.Text != "None" ) CalculateTotal(); else MessageBox.Show("Make sure you select an item in the combo box"); } |
private void txtTaxRate_Leave(object sender, System.EventArgs e) { CalculateTotal(); } |
private void btnClose_Click(object sender, System.EventArgs e) { Close(); } |
private void btnReset_Click(object sender, System.EventArgs e) { // Reset the controls this.txtCustomerName.Text = ""; this.txtCustomerPhone.Text = ""; this.txtShirtsUnitPrice.Text = "1.25"; this.txtShirtsQuantity.Text = "0"; this.txtShirtsSubTotal.Text = "0.00"; this.txtPantsUnitPrice.Text = "1.95"; this.txtPantsQuantity.Text = "0"; this.txtPantsSubTotal.Text = "0.00"; this.cboItem1.Text = "None"; this.txtItem1UnitPrice.Text = "0.00"; this.txtItem1Quantity.Text = "0"; this.txtItem1SubTotal.Text = "0.00"; this.cboItem2.Text = "None"; this.txtItem2UnitPrice.Text = "0.00"; this.txtItem2Quantity.Text = "0"; this.txtItem2SubTotal.Text = "0.00"; this.cboItem3.Text = "None"; this.txtItem3UnitPrice.Text = "0.00"; this.txtItem3Quantity.Text = "0"; this.txtItem3SubTotal.Text = "0.00"; this.cboItem4.Text = "None"; this.txtItem4UnitPrice.Text = "0.00"; this.txtItem4Quantity.Text = "0"; this.txtItem4SubTotal.Text = "0.00"; this.txtCleaningTotal.Text = "0.00"; this.txtTaxRate.Text = "5.75"; this.txtTaxAmount.Text = "0.00"; this.txtNetPrice.Text = "0.00"; this.txtCustomerName.Focus(); } |
using System.IO; using System.Xml; using System.Globalization; |
private void btnSave_Click(object sender, System.EventArgs e) { // This is the name of the file that holds or will hold the cleaning orders string strFilename = "CleaningOrders.xml"; XmlDocument xmlDoc = new XmlDocument(); // Find out if the file exists already // If it doesn't, then create it if( !File.Exists(strFilename) ) { xmlDoc.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "<CleaningOrders></CleaningOrders>"); xmlDoc.Save(strFilename); } // Open the XML file xmlDoc.Load(strFilename); // The count variable will allow us to either create // a receipt number or increment the current receipt number int count = 1; // Locate the CleaningOrder element that holds an individual entry XmlNodeList lstOrders = 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 ) count = lstOrders.Count + 1; // Create a cleaning order element XmlElement elmXML = xmlDoc.CreateElement("CleaningOrder"); // Add a receipt number as an attribute of the order elmXML.SetAttribute("ReceiptNumber", count.ToString()); // Create the child elements of a cleaning order string strNewCleaning = "<CustomerName>" + this.txtCustomerName.Text + "</CustomerName>" + "<CustomerPhone>" + this.txtCustomerPhone.Text + "</CustomerPhone>" + "<DateLeft>" + this.dtpDateLeft.Value.ToString() + "</DateLeft>" + "<TimeLeft>" + this.dtpTimeLeft.Value.ToString() + "</TimeLeft>" + "<DateExpected>" + this.dtpDateExpected.Value.ToString() + "</DateExpected>" + "<TimeExpected>" + this.dtpTimeExpected.Value.ToString() + "</TimeExpected>" + "<ShirtsUnitPrice>" + this.txtShirtsUnitPrice.Text + "</ShirtsUnitPrice>" + "<ShirtsQuantity>" + this.txtShirtsUnitPrice.Text + "</ShirtsQuantity>" + "<ShirtsSubTotal>" + this.txtShirtsSubTotal.Text + "</ShirtsSubTotal>" + "<PantsUnitPrice>" + this.txtShirtsUnitPrice.Text + "</PantsUnitPrice>" + "<PantsQuantity>" + this.txtPantsQuantity.Text + "</PantsQuantity>" + "<PantsSubTotal>" + this.txtPantsSubTotal.Text + "</PantsSubTotal>" + "<Item1>" + this.cboItem1.Text + "</Item1>" + "<Item1UnitPrice>" + this.txtItem1UnitPrice.Text + "</Item1UnitPrice>" + "<Item1Quantity>" + this.txtItem1Quantity.Text + "</Item1Quantity>" + "<Item1SubTotal>" + this.txtItem1SubTotal.Text + "</Item1SubTotal>" + "<Item2>" + this.cboItem2.Text + "</Item2>" + "<Item2UnitPrice>" + this.txtItem2UnitPrice.Text + "</Item2UnitPrice>" + "<Item2Quantity>" + this.txtItem2Quantity.Text + "</Item2Quantity>" + "<Item2SubTotal>" + this.txtItem2SubTotal.Text + "</Item2SubTotal>" + "<Item3>" + this.cboItem3.Text + "</Item3>" + "<Item3UnitPrice>" + this.txtItem3UnitPrice.Text + "</Item3UnitPrice>" + "<Item3Quantity>" + this.txtItem3Quantity.Text + "</Item3Quantity>" + "<Item3SubTotal>" + this.txtItem3SubTotal.Text + "</Item3SubTotal>" + "<Item4>" + this.cboItem4.Text + "</Item4>" + "<Item4UnitPrice>" + this.txtItem4UnitPrice.Text + "</Item4UnitPrice>" + "<Item4Quantity>" + this.txtItem4Quantity.Text + "</Item4Quantity>" + "<Item4SubTotal>" + this.txtItem4SubTotal.Text + "</Item4SubTotal>" + "<CleaningTotal>" + this.txtCleaningTotal.Text + "</CleaningTotal>" + "<TaxRate>" + this.txtTaxRate.Text + "</TaxRate>" + "<TaxAmount>" + this.txtTaxAmount.Text + "</TaxAmount>" + "<NetPrice>" + this.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++; // Reset the form in case the user wants // to add a new cleaning order this.btnReset_Click(sender, e); } } |
private void btnOpen_Click(object sender, System.EventArgs e) { string strFilename = "CleaningOrders.xml"; XmlDocument xmlDoc = new XmlDocument(); // If the XML file cannot be found, let the user know and give up if( !File.Exists(strFilename) ) { MessageBox.Show("No cleaning order exists or the file cannot be found"); return; } // In case the file exists, open it xmlDoc.Load(strFilename); // Get a list of the elements whose values are CleaningOrder XmlNodeList lstOrders = xmlDoc.GetElementsByTagName("CleaningOrder"); // Check each cleaning order for(int i = 0; i < lstOrders.Count; i++) { // If you find a cleaning order with the receipt number if( lstOrders[i].Attributes["ReceiptNumber"].Value.Equals(this.txtReceiptNumber.Text) ) { // Once you find that cleaning order, get the value of each node and display // them in the form this.txtCustomerName.Text = lstOrders[i]["CustomerName"].InnerText; this.txtCustomerPhone.Text = lstOrders[i]["CustomerPhone"].InnerText; this.dtpDateLeft.Value = DateTime.Parse(lstOrders[i]["DateLeft"].InnerText); this.dtpTimeLeft.Value = DateTime.Parse(lstOrders[i]["TimeLeft"].InnerText); this.dtpDateExpected.Value = DateTime.Parse(lstOrders[i]["DateExpected"].InnerText); this.dtpTimeExpected.Value = DateTime.Parse(lstOrders[i]["TimeExpected"].InnerText); this.txtShirtsUnitPrice.Text = lstOrders[i]["ShirtsUnitPrice"].InnerText; this.txtShirtsQuantity.Text = lstOrders[i]["ShirtsQuantity"].InnerText; this.txtShirtsSubTotal.Text = lstOrders[i]["ShirtsSubTotal"].InnerText; this.txtPantsUnitPrice.Text = lstOrders[i]["PantsUnitPrice"].InnerText; this.txtPantsQuantity.Text = lstOrders[i]["PantsQuantity"].InnerText; this.txtPantsSubTotal.Text = lstOrders[i]["PantsSubTotal"].InnerText; this.cboItem1.Text = lstOrders[i]["Item1"].InnerText; this.txtItem1UnitPrice.Text = lstOrders[i]["Item1UnitPrice"].InnerText; this.txtItem1Quantity.Text = lstOrders[i]["Item1Quantity"].InnerText; this.txtItem1SubTotal.Text = lstOrders[i]["Item1SubTotal"].InnerText; this.cboItem2.Text = lstOrders[i]["Item2"].InnerText; this.txtItem2UnitPrice.Text = lstOrders[i]["Item2UnitPrice"].InnerText; this.txtItem2Quantity.Text = lstOrders[i]["Item2Quantity"].InnerText; this.txtItem2SubTotal.Text = lstOrders[i]["Item2SubTotal"].InnerText; this.cboItem3.Text = lstOrders[i]["Item3"].InnerText; this.txtItem3UnitPrice.Text = lstOrders[i]["Item3UnitPrice"].InnerText; this.txtItem3Quantity.Text = lstOrders[i]["Item3Quantity"].InnerText; this.txtItem3SubTotal.Text = lstOrders[i]["Item3SubTotal"].InnerText; this.cboItem4.Text = lstOrders[i]["Item4"].InnerText; this.txtItem4UnitPrice.Text = lstOrders[i]["Item4UnitPrice"].InnerText; this.txtItem4Quantity.Text = lstOrders[i]["Item4Quantity"].InnerText; this.txtItem4SubTotal.Text = lstOrders[i]["Item4SubTotal"].InnerText; this.txtCleaningTotal.Text = lstOrders[i]["CleaningTotal"].InnerText; this.txtTaxRate.Text = lstOrders[i]["TaxRate"].InnerText; this.txtTaxAmount.Text = lstOrders[i]["TaxAmount"].InnerText; this.txtNetPrice.Text = lstOrders[i]["NetPrice"].InnerText; } } } |
|
||
Home | Copyright © 2004-2012, FunctionX | |
|